Puzzle S2

Is a character whitespace?

[E-1]Write a function isWhitespace() that determines if a character (contained in an int) is a whitespace character. Whitespace is any of the following characters: space, tab, carriage return, newline, vertical tab, or form feed. Here is a testing program:

/* Puzzle S02 --  is a character whitespace? */

#include <stdio.h>
#include <stdlib.h>

int isWhitespace( int ch )
{
}

int main()
{
  char trials[] = {'a', 'B', ' ', '+', 'z', 0x33, 0x09, 0x0A, 0x0B, 0x0C, 0x0D };
  int j, ch;
  
  for ( j=0; j<sizeof(trials); j++ )
  {
    ch = (int)trials[j] ; /* char in low order byte */
    printf("%02X ", ch );
    if ( isWhitespace( ch ) )
      printf("is whitespace\n");
    else
      printf("is NOT whitespace\n");
 }
   
 return 0;
}

Consult the table of ASCII in the introduction to determine the bit patterns for the whitespace characters.

Note: this function is similar to the function isspace() described in <ctype.h>. In professional-level code, use the standard function rather than your own.



Previous Page        Answer         Next Page         Home