strchr() function in C/C++


The function strchr() is used to search the character in the string. It search the first occurrence of character which is passed as second argument and it returns a pointer to the character, if successful otherwise, NULL.

Here is the syntax of strchr() in C language,

char *strchr(const char *string , int character)

Here,

string − The string which is to be scanned to search the character.

character − The character which is to be searched in the string.

Here is an example of strchr() in C language,

Example

 Live Demo

#include<stdio.h>
#include<string.h>
int main() {
   char s[] = "Helloworld!";
   char c = 'o';
   char *result = strchr(s, c);
   printf("String after searching the character : %s", result);
   return 0;
}

Output

String after searching the character : oworld!

In the above program, a char type string is passed and a character which is to be searched in the string. The result is stored in pointer variable *result.

char s[] = "Helloworld!";
char c = 'o';
char *result = strchr(s, c);

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 26-Jun-2020

262 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements