strrchr() — Find last occurrence of character in string
Standards
Standards / Extensions | C or C++ | Dependencies |
---|---|---|
ISO C
POSIX.1 XPG4 XPG4.2 C99 Single UNIX Specification, Version 3 |
both |
Format
#include <string.h>
char *strrchr(const char *string, int c);
General description
The strrchr() function finds the last occurrence of c (converted to a char) in string. The ending NULL character is considered part of the string.
Returned value
If successful, strrchr() returns a pointer to the last occurrence of c in string.
If the given character is not found, strrchr() returns a NULL pointer.
Example
CELEBS49
/* CELEBS49
This example compares the use of &strchr. and &strrchr..
It searches the string for the first and last occurrence of
p in the string.
*/
#include <stdio.h>
#include <string.h>
#define SIZE 40
int main(void)
{
char buf[SIZE] = "computer program";
char * ptr;
int ch = 'p';
/* This illustrates strchr */
ptr = strchr( buf, ch );
printf( "The first occurrence of %c in '%s' is '%s'\n", ch, buf, ptr );
/* This illustrates strrchr */
ptr = strrchr( buf, ch );
printf( "The last occurrence of %c in '%s' is '%s'\n", ch, buf, ptr );
}
Output
The first occurrence of p in 'computer program' is 'puter program'
The last occurrence of p in 'computer program' is 'program'