C examples for string.h:strchr
function
<cstring> <string.h>
Locate first occurrence of character in string
const char * strchr ( const char * str, int character ); char * strchr ( char * str, int character );
Parameter | Description |
---|---|
str | C string. |
character | Character to be located. |
A pointer to the first occurrence of character in str. If not found, the function returns a null pointer.
#include <stdio.h> #include <string.h> int main ()// ww w . ja v a2 s .c o m { char str[] = "This is a sample string"; char * pch; printf ("Looking for the 's' character in \"%s\"...\n",str); pch=strchr(str,'s'); while (pch!=NULL) { printf ("found at %d\n",pch-str+1); pch=strchr(pch+1,'s'); } return 0; }