#include <string.h> char *strrchr(const char *str, int ch);
strrchr
searches an input string str
for the last occurrence of a
search character ch
. The strrchr
function is the reverse of strchr
.
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.
#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); }
strchr
, strrcspn
, strrspn
Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.