Header files: #include <string.h>
STRCHR () is used to find the first occurrence of a character in a string, and its prototype is:
char * STRCHR (const char *STR, int c);
"Parameter" STR is the string to find, and C is the character to look for.
STRCHR () will find the address of the first occurrence of the character C in the STR string and return the address.
Note: The end flag of the string str NUL will also be included in the search scope, so a character from the STR group can also be positioned.
Return value returns the address of the character if the specified character is found, otherwise NULL is returned.
The address returned is the randomly assigned address of the string in memory plus the character you are searching for in the string position. The first occurrence of the set character in the string is I, then the returned address can be interpreted as STR + i.
Tip: If you want to find the last occurrence of a character in a string, you can use the STRRCHR () function.
"Instance" looks for the first occurrence of character 5.
Plain Text New window
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main(){
- char *s = "0123456789012345678901234567890";
- Char *p;
- P = strchr(S, ' 5 ');
- printf("%ld\ n", S);
- printf("%ld\ n", p);
- System("pause");
- return 0;
- }
Output Result:
12016464
12016469
C language STRCHR () function: Find where a character first appears in a string