strrspn -- Locate the Last Character of a Search Set Not in a Given Set

SYNOPSIS

 #include <lcstring.h>

 size_t strrspn(const char *str, const char *cset);
 

DESCRIPTION

strrspn locates the last character in the input string str not contained in the search set cset. The strrspn function is the reverse of strspn.

RETURN VALUE

strrspn returns the number of characters in the input string up to and including the last occurrence of a character not in the search set.

If all characters of the input string are in the search set, the return value is 0. If the search set is null (that is, if it contains no characters), the return value from strrspn is the length of the input string.

CAUTION

A protection or addressing exception may occur if either argument is not terminated with the null character.

EXAMPLE

This example uses strrspn to remove trailing blanks from the end of a line:
  #include <lcstring.h>
  #include <stdio.h>

  #define MAXLINE 80

  main()
  {
     char *line;
     char string[MAXLINE];
     size_t i;

     puts("Enter a string, followed by some spaces:");
     line = gets(string);

        /* Find the position (i) of the last character   */
        /*  in the string that is not a blank.           */
     i = strrspn(line, " ");

        /* Check if line is a null string.               */
     if (i > 0)

           /* Set the character after the last           */
           /*  nonblank character to the null character. */
        line[i] = '0';

     else
        puts("The string contains only blanks.");

         printf("The last nonblank character in the string is %cn",
                 line[i-1]);
  }

 

RELATED FUNCTIONS

strrcspn, strspn

SEE ALSO

String Utility Functions

Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.