This documentation is copyright © 1998-2001 Sandro Sigala <sandro@sigala.it>.
All rights reserved.

Released under the GNU General Public License.

Return to index Return to header Previous symbol Next symbol

strrchr

Prototype

#include <string.h>

char *strrchr(const char *str, int c);

Description

Finds in the specified string str, starting from the end, the specified character c. Returns a pointer to the first character found, otherwise NULL.

Implementation

View source
#include <string.h>

char *strrchr(const char *str, int c)
{
    const char *p = NULL;

    for (; *str != '\0'; ++str)
	if (*str == c)
	    p = str;

    return (char *)p;
}

Example

View source
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    char *s = "Hi Hello", *p;

    p = strchr(s, 'H');
    printf("%s\n", p);		/* Prints "Hi Hello". */
    p = strrchr(s, 'H');
    printf("%s\n", p);		/* Prints "Hello". */

    return EXIT_SUCCESS;
}

References

ISO C 9899:1990 7.11.5.5