Ad

Tuesday, July 17, 2007

Changing the case (lower, upper) of Strings

In this article, we will be designing two functions to change the case of strings. One would change a string from lower case to upper case while the other would do the opposite.

Although we have pre-defined functions for doing this in a header file, but this article is for those who dare to know how all these operations are done internally.

Changing the case: How is it done?

The main theory lies in the way C++ treats character constants and strings. Have a look at the following code:

  #include<iostream.h>

  void main(void)
  {
   char first='A';
   char second=65;

   cout<<first;
   cout<<endl;

   cout<<second;
   cout<<endl;
  }

whose output is:

   A
   A
   Press any key to continue

This is because ‘A’ and its ASCII code 65 are equivalent to the compiler and in c++ we can manipulate it in whatever way we like.

Now look at the following code:

  #include<iostream.h>

  void main(void)
  {
   char arr[4]="ABC";
   char arr2[4]={65,66,67};

   cout<<arr;
   cout<<endl;

   cout<<arr2;
   cout<<endl;
  }

Whose output is (yeah you guessed it right!):

   ABC
   ABC
   Press any key to continue

So this proves that strings can also be expressed (manipulated) by ASCII codes.

ASCII code of some characters:

   A: 65    a: 97
   B: 66    b: 98
   C: 67    c: 99
   …        …
   …        …

From the above, we can conclude that by increasing or decreasing the ASCII codes of a character by 32, we can change its case. Just as shown in the following code:

  #include<iostream.h>

  void main(void)
  {
   char chr='A';
   char chr2='b';

   cout<<chr;
   cout<<endl;

   chr=chr + 32;
   cout<<chr;
   cout<<endl<<endl;

   cout<<chr2;
   cout<<endl;

   chr2=chr2 - 32;
   cout<<chr2;
   cout<<endl;
  }

OUTPUT:

   A
   a

   b
   B
   Press any key to continue

This theory can also be applied to strings.

Now, that you know the main theory behind we can jump straight to the example program to illustrate all this:

Keep reading the comments though!

  // C++ example program to show
  // how case(uppercase and lowercase)
  // of strings can be changed from
  // one to the other
  #include<iostream.h>

  void to_upper(char *);
  void to_lower(char *);

  void main(void)
  {
   char str[50]="I Love C++. The number 1 language!";

   to_upper(str);
   cout<<str;

   to_lower(str);
   cout<<endl;
   cout<<str;

   cout<<endl;
  }

  // takes a character array
  // as argument and changes
  // it to upper case
  // NOTE: special symbols and
  // numbers remains the same
  void to_upper(char *str)
  {
   // while end of the string
   // has not been reached
   while(*str!='\0')
   {
    // change only if its a
    // lower case character
    // intelligent enough not to
    // temper with special
    // symbols and numbers
    if(*str>=97 && *str<=122)
      *str-=32;

    str++;
   }
  }

  // takes a character array
  // as argument and changes
  // it to lower case
  // NOTE: special symbols and
  // numbers remains the same
  void to_lower(char *str)
  {
   while(*str!='\0')
   {
    // change only if its a
    // UPPER case character
    // intelligent enough not to
    // temper with special
    // symbols and numbers
    if(*str>=65 && *str<=90)
      *str+=32;

    str++;
   }
  }

Good-Bye!

2 comments:

  1. Jimmy Ilenloa [iilenloa@gmail.com]
    Thank you very much. Your site is actually making me remember the things in C++ I forgot.

    ReplyDelete

You are free to comment anything, although you can comment as 'Anonymous' it is strongly recommended that you supply your name. Thank You.

Please don't use abusive language.