Strchr
char* strchr(char *s, int c)
{
    char *p = s;
    while (*p != c && *p != '\0')
        p++;
    if (*p == '\0')
        return NULL;
    else
        return p;  // return char pointer which points to the found location 
}
 
int main () 
{  
    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;
}

Looking for the 's' character in "This is a sample string"…
found at 4
found at 7
found at 11
found at 18

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License