Puzzle S1

Is a character upper case?

[E-1]Write a function that determines if a character is one of the upper case characters 'A' through 'Z'. If so, return 1, otherwise return zero. Here is a prototype for the function:

int isUppercase( int ch );

The argument for the function is of type int. The eight bits of the character itself will be in the low order byte of the argument.

Here is a skeleton of a testing program. Finish it by completing the function. Check your answer in your C environment or an online C compiler.


/* Puzzle S01 -- detect upper case characters */

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

int isUppercase( int ch )
{
  . . . .
}

int main()
{
  int trials[] = {'a', 'B', 'Z', ' ', '+', 'z', 'Z', 0x33 };
  int j, ch;
  
  for ( j=0; j<sizeof(trials)/sizeof(int); j++ )
  {
    ch = trials[j];
    if ( isUppercase( ch ) )
      printf("%c is upper case\n", ch);
    else
      printf("%c is NOT upper case\n", ch);
  }
    
  return 0;
}

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



Previous Page        Answer         Next Page         Home