Chapter Contents

Previous

Next
strrchr

strrchr



Locate the Last Occurrence of a Character in a String

Portability: ISO/ANSI C conforming, UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
CAUTION
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <string.h>

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


DESCRIPTION

strrchr searches an input string str for the last occurrence of a search character ch . The strrchr function is the reverse of strchr .


RETURN VALUE

strrchr returns a character pointer to the last occurrence of the search character in the input string, or NULL if the character is not found. If the search character is the null character ('\0'), the return value addresses the null character at the end of the input string.


CAUTION

A protection or addressing exception may occur if the input string is not properly terminated with the null character.


EXAMPLE

#include <string.h>
#include <stdio.h>

#define MAXLINE 80

main()
{
   char text[MAXLINE];
   char *last_blank;

   puts("Enter some text.  Do not include trailing blanks.");
   gets(text);
   last_blank = strrchr(text, ' '); /* Find the last blank. */
   if (last_blank == NULL)
      puts("Your input was only a single word.");
   else if (*(last_blank+1) == '\0')
      puts("Your input included a trailing blank.");
   else
      printf("The last word in your input was \"%s\".",
             last_blank+1);
}


RELATED FUNCTIONS

strchr , strrcspn , strrspn


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.