C strrchr function searches string from the right
Syntax
C strrchr function has the following format.
char *strrchr(const char *str, int ch);
Header
C strrchr function
is from header file string.h
.
Description
C strrchr function returns a pointer to the last occurrence of ch
in *str
.
If no match is found, a null pointer is returned.
Example
Use C strrchr function to search string from the end.
#include <string.h>
#include <stdio.h>
// w w w. ja va 2 s . c om
int main(void){
char *p;
p = strrchr("this is a test", 'i');
printf(p);
return 0;
}
The code above generates the following result.
Using strchr in if statement.
#include <stdio.h>
#include <string.h>
/*from w ww.j av a 2 s . c o m*/
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;
}
The code above generates the following result.