+92 332 4229 857 99ProjectIdeas@Gmail.com

How to convert string into upper case (C++ Tutorial)


Convert string to upper case
Convert2Uppercase() takes a string as an input parameter and returns the string in its upper case representation.
Summary of Convert2Uppercase ()
There is a loop which iterates from the 0th index of the string to the last index, if it finds the character in its lower case representation – it converts that character to its upper case representation by subtracting 32 to the current character because subtracting 32 will give upper case representation of the character.
For example
a 97        97 - 32     =     A 65
b 98        98 - 32     =     B 66
c 99        99 - 32     =     C 67
.           .                 .
.           .                 .                
.           .                 .
z 122       122 - 32    =     Z 90

Convert2Uppercase() uses 2 functions:
IsAlphabat()
It returns true if the input character is an alphabat and returns false in case if the character is not an alphabat.
IsAlphabatSmall()
It returns true if the input alphabat is small and returns false in case if the alphabat is not small.

Code

#include "stdafx.h"
#include "iostream"
#include "conio.h"

using namespace std;

bool IsAlphabat( char ch )
{
      if ( ( ch >= 65 & ch <= 90 ) | ( ch >= 97 & ch <= 122 ) )
            return true;
      return false;
}

bool IsAlphabatSmall( char ch )
{
      if ( ch >= 97 & ch <= 122 )
            return true;
      return false;
}

char * Convert2Uppercase( char str[] )
{
      for ( int i = 0; i < strlen( str ); i++ )
      {
            if ( IsAlphabat( str[i] ) )
            {
                  if ( IsAlphabatSmall( str[i] ) )
                        str[i] = str[i] - 32;
            }
      }
      return str;
}

int main()
{
      char str[] = "Hello!!! my name is Saad Bin Saulat";
      cout << "Original  string : " << str << endl;
      cout << "Converted string : " << Convert2Uppercase( str ) << endl;

      _getche();
      return 0;
}

Output
Original  string : Hello!!! my name is Saad Bin Saulat
Converted string : HELLO!!! MY NAME IS SAAD BIN SAULAT

0 comments: