Name

strspn

Synopsis

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

#include <string.h>
intstrspn( 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).

Example

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] );
else
{
  sscanf( wordin, "%lg", &val );
  printf( "You entered the value %g\n", val );
}

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.