Puzzle S5

Convert a string to lower case

[E-4] Write a function that converts each upper case character of a string to lower case. If a character is not upper case, it is not altered. Here is a testing program:

/* Puzzle S05 --  string to lower case */
#include <stdio.h>
#include <stdlib.h>

int toLowercase( int ch )
{
}

void strToLower( char *p )
{
}

int main()
{
  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]  );
  }

  return 0;
}

Note: The array trials contains six strings (of up to 99 characters each). These strings are not constants, so they can be altered by strToLower(). The following testing program would NOT work, because it attempts to alter a constant string literal:

 
int main()
{
  char trial = "Immutable string LITERAL!" ;
  printf("Original : %s\n",   trial );
  strToLower( trial );                   /* Run-time error */
  printf("Converted: %s\n", trial );
}

However, with some C compilers the above defective program actually will compile and run without reported error.



Previous Page        Answer         Next Page         Home