C++ strchr()

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 << "\"";
        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?