+92 332 4229 857 99ProjectIdeas@Gmail.com

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


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

Convert2Lowercase() 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.
IsAlphabatCapital()
It returns true if the input alphabat is capital and returns false in case if the alphabat is not capital.

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 IsAlphabatCapital( char ch)
{
      if ( ch >= 65 & ch <= 90 )
            return true;
      return false;
}

char * Convert2Lowercase( char str[] )
{
      for ( int i = 0; i < strlen( str ); i++ )
      {
            if ( IsAlphabat( str[i] ) )
            {
                  if ( IsAlphabatCapital( 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 : " << Convert2Lowercase( 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: