C programming strchr string.h


In C programming the <string.h> strchr function search for a specific character in a string.The declaration of the function us given below.

char *strchr(const char *s, int c);

Parameters:
s -The string from which the character is to be searched for.

c -The character which is to be searched for in ‘s’ pointed string.

Return type
char* -Returns a pointer to the located character,or a null pointer if the character does not occur in the string.

This function is similar to ‘memchr’ function which also search for a particular character in a string.

Code example

char s[] = “Core C plus plus tutorial” , *retCh ,
c = ‘ ‘;

printf(“s=%s” , s );

//searches for ‘ ‘(space) and replaced it with ‘_'(underscore)
while( (retCh=strchr(s , c))!=NULL )
{
*retCh=’_’ ;
}

printf(“s=%s” , s);

Output,

s=Core C plus plus tutorial
s=Core_C_plus_plus_tutorial


Related links

->C memchr string.h

->C strcspn string.h