/* Puzzle S02 -- detect upper case characters */ #include <stdio.h> #include <stdlib.h> /* Puzzle S02 -- detect white case characters */ int isWhitespace( int ch ) { return ( ch == 0x20 || ch>= 0x09 && ch<= 0x0D ) ; } int main(int argc, char *argv[]) { 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"); } system("PAUSE"); return 0; }
Comments:
(1) The whitespace characters consist of space 0x20 and the range of characters 0x09 .. 0x0D. There are minor variations in what different C libraries count as whitespace, so be cautious.
(2) The testing program uses the format code "%02X" to print out the characters to show their ascii code with two digits of hexadecimal.
(3) The array is now an array of char
.
sizeof(trials)
gives the number of bytes,
which is now also the number of cells.