Previous Page
Next Page


Searches a string for a character that is not in a given set

#include <string.h>
int strspn ( const char *s1 , const char *s2  );

The strspn( ) function returns the index of the first character in the string addressed by s1 that does not match any character in the string addressed by s2, or in other words, the length of the string segment addressed by s1 that contains only characters that are present in the string addressed by s2. If all characters in s1 are also contained in s2, then strspn( ) returns the index of s1's string terminator character, which is the same as strlen(s1).


char wordin[256];
double val;

puts( "Enter a floating-point number, please:" );
scanf( "%s", wordin );

int index = strspn( wordin, "+-0123456789eE." );
if ( index < strlen( wordin ) )
  printf( "Sorry, but the character %c is not permitted.\n",
           wordin[index] );
  sscanf( wordin, "%lg", &val );
  printf( "You entered the value %g\n", val );

See Also

strcspn( ), wcsspn( )

Previous Page
Next Page