English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C / C ++The strchr() function in C

This functionstrchr()Used to search for a character in a string. It searches for the first occurrence of the character passed as the second parameter and returns a pointer to the character, otherwise returns NULL.

This isstrchr()C language syntax,

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

Here,

string-The string to be scanned to search for the character.

Character-The character to be searched in the string.

This isstrchr()C language example,

Example

#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 result

String after searching the character: oworld!

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

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