The C++ cstring strchr() for the first occurrence of a character in a string and returns a pointer to it.
Syntax:
char* strchr(const char* str, int character);
Parameters:
str: The string to search.
character: The character to find.
Example Usage:
#include
#include
int main() {
const char* text = "C++ programming is fun.";
char search = 'g';
char* result = strchr(text, search);
if (result != nullptr) {
std::cout << "Character found at: " << result << std::endl;
} else {
std::cout << "Character not found." << std::endl;
}
return 0;
}
Key Points:
Returns a pointer to the first occurrence of the character or nullptr if not found.
The search includes the null terminator, so a character of \0 will locate the string's end.