Ascertains whether a given character is a space or tab character #include <ctype.h> int isblank ( int c ); The function isblank( ) is a recent addition to the C character type functions. It returns a nonzero value (that is, TRue) if its character argument is either a space or a tab character. If not, the function returns 0 (false). ExampleThis program trims trailing blank characters from the user's input:
#define MAX_STRING 80
char raw_name[MAX_STRING];
int i;
printf( "Enter your name, please: " );
fgets( raw_name, sizeof(raw_name), stdin );
/* Trim trailing blanks: */
i = ( strlen(raw_name) - 1 ); // Index the last character.
while ( i >= 0 ) // Index must not go below first character.
{
if ( raw_name[i] == '\n' )
raw_name[i] = '\0'; // Chomp off the newline character.
else if ( isblank( raw_name[i] ) )
raw_name[i] = '\0'; // Lop off trailing spaces and tabs.
else
break; // Real data found; stop truncating.
--i; // Count down.
}
See also the example for isprint( ) in this chapter. See AlsoThe corresponding C99 function for wide characters, iswblank( ); isalnum( ), isalpha( ), iscntrl( ), isdigit( ), isgraph( ), islower( ), isprint( ), ispunct( ), isspace( ), isupper( ), isxdigit( ) |