Chapter Contents

Previous

Next
strspn

strspn



Locate the First Occurrence of the First Character Not in a Set

Portability: ISO/ANSI C conforming, UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
CAUTION
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <string.h>

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


DESCRIPTION

strspn locates the first character in the argument string str not contained in the argument string cset , returning its position in str .


RETURN VALUE

strspn returns the number of consecutive characters in the character set cset found in the argument string str , starting at the first character in str . If all characters of the string are in the set (so that no character not in the set is found), the value returned is the length of the string. If the character set is null (that is, if it contains no characters), the return value from strspn is 0.


CAUTION

A protection or addressing exception may occur if either argument is not properly terminated.

See the memscntb function description for information on possible interactions between the strspn , memscntb , or strscntb functions.


EXAMPLE

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

#define MAXLINE 40

main()
{
   char line[MAXLINE];
   char *word;
   size_t a,b;

   puts("Enter a word (only alphabetic characters):");
   word = gets(line);

      /* Find the position of the first character */
      /*  in the word that is not a vowel.        */
   a = strspn(word, "aeiou");

      /* Find the position of the first character */
      /*  in the word that is not a consonant.    */
   b = strspn(word, "bcdfghjklmnpqrstvwxyz");
   printf("The first consonant in the given word is: %c\n",
           word[a]);
   printf("The first vowel in the given word is: %c\n",
           word[b]);
}


RELATED FUNCTIONS

strcspn , strrspn , strscan


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

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