S05 Answer


/* Puzzle S05 -- string to upper 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;
}

void strToLower( char *p )
{
  while ( *p )
  {
    *p = toLowercase( *p );
    p++ ;
  }
  
}

int main(int argc, char *argv[])
{
  char trials[6][100] = {"UPPER CASE + lower case", "X", "", "Yet another string",
                    "End Of\nLines Should\nWork Fine.", "The game is afoot!"};
  int j ;

  for ( j=0; j<6; j++ )
  {
    printf("Original : %s\n",   trials[j]);
    strToLower( trials[j] );
    printf("Converted: %s\n\n", trials[j]  );
  }

  system("PAUSE");
  return 0;
}

Comments: There are several other ways to write the function. One version combines the functions of isUppercase() and toLowercase() into strToLowercase().