Convert characters of a string to opposite case

Given a string, convert the characters of the string into opposite case,i.e. if a character is lower case than convert it into upper case and vice-versa.
Examples:

Input : TechCodeBit
Output : tECHcODEbIT

Input : hello every one
Output : HELLO EVERY ONE

ASCII values  of alphabets: A – Z = 65 to 90, a – z = 97 to 122
Steps:

  1. Take one string of any length and calculate its length.
  2. Scan string character by character and keep checking  the index .
    • If character in a index is in lower case, then subtract 32 to convert it in upper case, else add 32 to convert it in upper case
  3. Print the final string.
// CPP program to Convert characters
// of a string to opposite case
#include<iostream>
using namespace std;
 
// Function to convert characters
// of a string to opposite case
void convertOpposite(string &str)
{
    int ln = str.length();
     
    // Conversion according to ASCII values
    for (int i=0; i<ln; i++)
    {
        if (str[i]>='a' && str[i]<='z')
        //Convert lowercase to uppercase
            str[i] = str[i] - 32;
        else if(str[i]>='A' && str[i]<='Z')
        //Convert uppercase to lowercase
            str[i] = str[i] + 32;
    }
}
 
// Driver function
int main()
{
    string str = "<a href="#">TechCodeBit</a>";
     
    // Calling the Function
    convertOpposite(str);
     
    cout << str;
    return 0;
}

Output:

tECHcODEbIT

 

Disclaimer: This does not belong to TechCodeBit, its an article taken from the below
source and credits.
source and credits: http://www.geeksforgeeks.org
We have built the accelerating growth-oriented website for budding engineers and aspiring job holders of technology companies such as Google, Facebook, and Amazon
If you would like to study our free courses you can join us at

http://www.techcodebit.com. #techcodebit #google #microsoft #facebook #interview portal #jobplacements
#technicalguide

rakesh

Leave a Reply

Your email address will not be published. Required fields are marked *

Skip to toolbar