Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C++ String strrchr() Function

Posted in C++ Tutorial
Updated on Oct 06, 2024
By Mari Selvan
👁ī¸ 213 - Views
âŗ 4 mins
đŸ’Ŧ 1 Comment
C++ String strrchr() Function

Photo Credit to CodeToFun

🙋 Introduction

In C++ programming, manipulating strings is a common and fundamental task.

The strrchr() function is a part of the C library and is used for searching a character in a string. Unlike other string functions, strrchr() searches for the last occurrence of a character in a given string.

In this tutorial, we'll explore the usage and functionality of the strrchr() function in C++.

💡 Syntax

The signature of the strrchr() function is as follows:

Syntax
Copied
Copy To Clipboard
char* strrchr(const char* str, int character);

This function takes a C-style string str and a character character and returns a pointer to the last occurrence of the character in the string. If the character is not found, it returns a null pointer.

📄 Example

Let's delve into an example to illustrate how the strrchr() function works.

strrchr.cpp
Copied
Copy To Clipboard
#include <iostream>
#include <cstring>

int main() {
  const char * sentence = "Hello, C++ programming!";

  // Find the last occurrence of 'o'
  const char * lastO = strrchr(sentence, 'o');

  if (lastO != nullptr) {
    std::cout << "Last occurrence of 'o' found at position: " << lastO - sentence << std::endl;
  } else {
    std::cout << "'o' not found in the string." << std::endl;
  }

  return 0;
}

đŸ’ģ Output

Output
Last occurrence of 'o' found at position: 13

🧠 How the Program Works

In this example, the strrchr() function is used to find the last occurrence of the character 'o' in the string "Hello, C++ programming!" and prints the position.

↩ī¸ Return Value

The strrchr() function returns a pointer to the last occurrence of the specified character in the string. If the character is not found, it returns a null pointer (nullptr).

📚 Common Use Cases

The strrchr() function is useful when you need to find the last occurrence of a specific character in a string. It can be applied in scenarios such as parsing file paths, extracting file extensions, or manipulating text data.

📝 Notes

  • The strrchr() function operates on C-style strings, and the character argument is specified as an integer. Ensure that the character is cast to an integer if it is specified as a character literal.

đŸŽĸ Optimization

The strrchr() function is optimized for searching the last occurrence of a character in a string. However, for performance-critical applications, you may want to consider alternative approaches or additional checks based on specific requirements.

🎉 Conclusion

The strrchr() function in C++ is a valuable tool for searching the last occurrence of a character in a string. It provides a standardized and efficient way to perform this task, contributing to the versatility of string manipulation in C++.

Feel free to experiment with different strings and characters to explore the behavior of the strrchr() function in various scenarios. Happy coding!

👨‍đŸ’ģ Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
9 months ago

If you have any doubts regarding this article (C++ String strrchr() Function), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy