S03 Answer


/* Puzzle S03 -- to lower case */

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

int isUppercase( int ch )
{
  return ch>='A' && ch<='Z' ;
}

int toLowercase( int ch )
{
  if ( isUppercase( ch ) )
    return ch + 0x20;
  else
    return ch;
}

int main(int argc, char *argv[])
{
  char trials[] = {'a', 'B', 'C', '+', 'z', '0', '?', 0x0D };
  int j, ch;

  for ( j=0; j<sizeof(trials); j++ )
  {
    ch = (unsigned int)trials[j] ; /* char in low order byte */
    printf("%c ", ch );
    printf("to lower case: %c\n", toLowercase(ch) );
  }

  system("PAUSE");
  return 0;
}

Comments: For greater efficiency, you might want toLowercase() to include the code that detects when a character is upper case. Doing this would elimiate one function call.

int toLowercase( int ch )
{
  if ( ch>='A' && ch<='Z' )
    return ch + 0x20;
  else
    return ch;
}

There are several functions that are standard in C environments that convert characters:

Function
Description
int toascii(int ch) Discard all but the low-order 7 bits of ch, thus insuring that the low-order byte contains one of the ASCII codes 0x00 ... 0x7F
int toint(int ch) Convert the ASCII digit ch 0x30..0x39 into an int 0..9
int tolower(int ch) If ch is upper case, convert it into a lower case character.
int toupper(int ch) If ch is lower case, convert it into an upper case character.