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);
Updated on: 2020-06-26T08:03:47+05:30

376 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements