December 2005
Beginner to intermediate
618 pages
20h 19m
English
isblank
Ascertains whether a given character is a space or tab character
#include <ctype.h> intisblank( intc);
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).
This 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.
Read now
Unlock full access