C Tutorial/string.h/strrchr
strrchr
Item Value Header file string.h Declaration char *strrchr(const char *str, int ch); Return returns a pointer to the last occurrence of ch in *str.
If no match is found, a null pointer is returned.
<source lang="cpp">#include <string.h>
#include <stdio.h> int main(void){ char *p; p = strrchr("this is a test", "i"); printf(p); return 0; }</source>
Using strchr
<source lang="cpp">#include <stdio.h>
- include <string.h>
int main() {
const char *string = "This is a test"; char character1 = "a"; char character2 = "z"; if ( strchr( string, character1 ) != NULL ) { printf( "\"%c\" was found in \"%s\".\n", character1, string ); } else { printf( "\"%c\" was not found in \"%s\".\n", character1, string ); } if ( strchr( string, character2 ) != NULL ) { printf( "\"%c\" was found in \"%s\".\n", character2, string ); } else { printf( "\"%c\" was not found in \"%s\".\n", character2, string ); } return 0;
}</source>
"a" was found in "This is a test". "z" was not found in "This is a test".