Author Topic: Uppercase to lowercase covertion  (Read 5758 times)

Qodeus

  • User
  • *
  • Posts: 1
Uppercase to lowercase covertion
« on: February 14, 2006, 05:39:43 PM »
Hello. My problem is that I do not know how to conver lowercase chars to uppercase. And the other way around.

Someone help, please. Thanks in advance!

louiecerv

  • Professor
  • User
  • *
  • Posts: 85
toupper() and tolower()
« Reply #1 on: February 16, 2006, 09:48:34 AM »
Hi!

you need to add a reference to the cctype library:
#include <cctype>

The output of the toupper() and tolower() functions is the integer equivalent of the character so you need to cast the return value to get the character.

Here's a program sample I whipped up for you to play with. If you need more info, just ask. Also check out cplusplus.com for tutorials.

Code: [Select]


#include <iostream>
#include <cctype>
#include <cstring>

using namespace std;
string uppercase(string input);
string lowercase(string input);
string titlecase(string input);

int main()
{
     string instring;
     cout << "A Program to demonstrate use of toupper() and tolower()" << endl;
     cout << "Enter a word or phrase then press ENTER : " << endl;
     
     int count = 0;
     char nextchar;
     do
     {
          cin.get(nextchar);
          instring[count] = nextchar;
          count++;
     } while (nextchar != '\n');
     instring[count+1] = '\0';
     
     cout << "All uppercase: " << uppercase(instring) << endl;
     cout << "All lowercase: " << lowercase(instring) << endl;
     cout << "Title case   : " << titlecase(instring) << endl;

     system("PAUSE");
     return 0;
}

string uppercase(string instring)
{
     int maxchars = strlen(instring.c_str() );
     char ch_array[maxchars];
     int i;
     for (i = 0; i < maxchars; i++)
     {
          ch_array[i] = static_cast<char>(toupper(instring[i]));          
     }
     ch_array[maxchars] = '\0';
     return ch_array;
}

string lowercase(string instring)
{
     int maxchars = strlen(instring.c_str() );
     char ch_array[maxchars];
     int i;
     for (i = 0; i < maxchars; i++)
     {
          ch_array[i] = static_cast<char>(tolower(instring[i]));          
     }
     ch_array[maxchars] = '\0';
     return ch_array;
}

string titlecase(string instring)
{
     int maxchars = strlen(instring.c_str() );
     char ch_array[maxchars];

     ch_array[0] = static_cast<char>( toupper(instring[0]) );          
     
     int i;
     for (i = 1; i < maxchars; i++)
     {
          if ( isspace(ch_array[i-1]) )
               ch_array[i] = static_cast<char>(toupper(instring[i]));          
          else
               ch_array[i] = instring[i];
     }
     
     ch_array[maxchars] = '\0';
     return ch_array;
}

Analyze. Design. Develop. Debug. Deploy. Maintain.

louiecerv

  • Professor
  • User
  • *
  • Posts: 85
barest sample
« Reply #2 on: February 16, 2006, 10:01:06 AM »
Code: [Select]

#include <iostream>
#include <cctype>

using namespace std;

int main()
{
     cout << "Simple character conversion" << endl;
     
     do
     {
     char letter;
     cout << "input a letter: ";
     cin >> letter;
     
     char upper = static_cast<char>( toupper(letter) );
     char lower = static_cast<char>( tolower(letter) );
     
     cout << "Uppercase : " << upper << endl;
     cout << "Lowercase : " << lower << endl;
     } while (true);
     system("PAUSE");
     return 0;
}

Analyze. Design. Develop. Debug. Deploy. Maintain.