Popular Tutorials
Start Learning C++Certification Courses
Created with over a decade of experience and thousands of feedback.
Python online compiler
JavaScript online compiler
SQL online compiler
Java online compiler
HTML online compiler
C online compiler
C++ online compiler
C# online compiler
PHP online compiler
Swift online compiler
Kotlin online compiler
TypeScript online compiler
Go online compiler
Rust online compiler
Scala online compiler
Dart online compiler
R online compiler
Ruby online compiler
C++ strchr()
The strchr() function in C++ searches for the first occurrence of a character in a string.
strchr() prototype
const char* strchr( const char* str, int ch ); char* strchr( char* str, int ch );
The strchr() function takes two arguments: str and ch. It searches for the character ch in the string pointed to by str.
It is defined in <cstring> header file.
strchr() Parameters
ptr: Pointer to the null terminated string to be searched for.ch: Character to search for.
strchr() Return value
If the character is found, the strchr() function returns a pointer to the location of the character in str, otherwise returns null pointer.
Example: How strchr() function works
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char str[] = "Programming is easy.";
char ch = 'r';
if (strchr(str, ch))
cout << ch << " is present \"" << str << "\"";
else
cout << ch << " is not present \"" << str << "\"";
return 0;
}
When you run the program, the output will be:
r is present "Programming is easy."
Did you find this article helpful?