Name

strrchr

Synopsis

Searches for the rightmost occurrence of a given character in a string

#include <string.h>
char *strrchr( const char *s, int c );

The strrchr() function returns a pointer to the last occurrence of the character value c in the string addressed by s. If there is no such character in the string, strrchr() returns a null pointer. If c is a null character ('\0'), then the return value points to the terminator character of the string addressed by s.

Example

char *mybasename =strrchr( argv[0], '/' );       // Find end of path.
if ( mybasename != NULL )
  mybasename++;             // Point to the first character after the slash.
else
  mybasename = argv[0];
printf( "This program was invoked as %s.\n", mybasename );

See Also

strchr(), strpbrk(), strstr(); the wide-string functions wcschr() and wcsrchr()

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.