Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C++ String strchr() Function

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

Photo Credit to CodeToFun

🙋 Introduction

In C++ programming, manipulating strings is a common task, and the strchr() function is a handy tool for working with null-terminated strings.

This function is part of the C Standard Library and is used to locate the first occurrence of a specified character within a string.

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

💡 Syntax

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

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

This function searches for the first occurrence of the specified character (an integer representing an ASCII value) within the null-terminated string str.

📄 Example

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

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

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

  // Search for the character 'C'
  const char * result = strchr(sentence, searchChar);

  // Output the result
  if (result != nullptr) {
    std::cout << "The character '" << searchChar << "' was found at position: " << result - sentence << std::endl;
  } else {
    std::cout << "The character '" << searchChar << "' was not found in the string." << std::endl;
  }

  return 0;
}

đŸ’ģ Output

Output
The character 'C' was found at position: 7

🧠 How the Program Works

In this example, the strchr() function searches for the character 'C' within the string "Hello, C++!" and prints the result.

↩ī¸ Return Value

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

📚 Common Use Cases

The strchr() function is particularly useful when you need to locate the position of a specific character within a string. It simplifies tasks such as extracting substrings or validating the presence of certain characters.

📝 Notes

  • The strchr() function is case-sensitive. If you need a case-insensitive search, consider using std::string and std::find() or implement a custom case-insensitive search.
  • For C++ strings, the find() method provides similar functionality and is often preferred for its flexibility.

đŸŽĸ Optimization

The strchr() function is optimized for efficiency, and no additional optimization is typically needed. Ensure that you handle the possibility of a null pointer if the character is not found.

🎉 Conclusion

The strchr() function in C++ is a valuable tool for searching for a specific character within null-terminated strings. It provides a standardized and efficient way to perform this task, enhancing the flexibility and readability of your code.

Feel free to experiment with different strings and characters to explore the behavior of the strchr() 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
10 months ago

If you have any doubts regarding this article (C++ String strchr() 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