strchr
Locate character in string
Description
The strchr function locates the first occurrence of c (converted to a char) in the string pointed to by s. The terminating null character is considered part of the string; therefore if c is '\0', the functions locate the terminating '\0'. The strrchr function is identical to strchr except it locates the last occurrence of c.Example:
Example - Locate character in string
Workings
#include <stdio.h> #include <string.h> int main() { // define some string char s[10] = "abcdefgh"; // look for the character 'e' in the string s char *pos = strchr(s, 'e'); // display search result if (pos) printf("Character 'e' found at position %d.\n", pos - s); else printf("Character 'e' not found.\n"); return 0; }
Solution
Output:
Character 'e' found at position 4.