# Convert uppercase to lowercase and vice versa

In this tutorial, we see a C Program to Convert String Lowercase to Uppercase And Vice Versa. What logic need for doing this.

What is upper case: letters A – Z are upper.

What is lowercase: letters a – z are lower.

In c , the ascii value of letters :

A = 65 , B = 66 , ….. , Z = 90.

a = 97 , b = 98 , …… , z = 122.

As we know the ascii values of letter based on that we convert upper case to lower case and vice versa.

The logic to convert uppercase to lowercase and vice versa:

To convert upper to lower: add 32 with an uppercase letter.

Example:

convert A to a

=> A + 32

=> 65 + 32

=> 97

And 97 is ascii value of a

The logic to convert lower to upper: subtract 32 with lower case letter.

Example:

convert a to A

=> a – 32

=> 97 – 32

=> 65

And 65 is ascii value of A

(Note :- when we add any number with letter , ascii value of letter get added to number and returns ascii number )

#### Algorithm :-

1. Start

2. Declare strings for taking input and another for storing converted string.

3. Take input string from user.

4. Read string character one by one till length of string

5. Compare letter is upper or lower letter.

6. If letter is upper , add 32 with letter and store in another string.

7. If letter is lower , subtract 32 with letter and store in another string.

8. String reached at end then stop.

9. Print second string.

10. End

### C Program to Convert String Lowercase to Uppercase And Vice Versa

#### Explanation :-

1. First, we declare the variable.

str1 => for taking string input from user.

str2 => for storing converted string

i => for iterating string.

2. Read string input from a user in str1.

gets: gets function is used to read string input from the user.

suppose user input

str1 = Let’s Learn C With Technical Seek Website

3. Next line is

for (i = 0 ; i < strlen (str1); i++)

{

}

This is to traverse the sentence from start to end. Starting from 0 positions and till the length of the sentence. For example string = technicalseek. The length of a string is 13 and for this, it will iterate 13 times.

And in our input sentence = Let’s Learn C With Technical Seek Website. The length of the sentence is 40 and it will iterate 40 times.

4. Next lines are

To convert upper to lower.

if ( str1[i] > ‘a’ && str1 [i] < ‘z’ )

{

str2 [i] = (char) str1 [i] – 32 ;

}

As we seen how to convert upper to lower i.e to subtract 32 in letter.

Next lines for converting lower to upper so we add 32 to letter.

if ( str1[i] > ‘a’ && str1 [i] < ‘z’ )

{

str2 [i] = (char) str1 [i] + 32 ;

}

If letter is not upper or lower i.e it’s number or anything other then copy letter as it is.

else {

str2 [i] = str1 [i] ;

}

In our input string :-

Let’s Learn C with Technical Seek Website.

First iteration i = 0 :

str1 = L

The letter is upper so second condition get true so add 32

str2  = L + 32

str2  = 76 + 32

str2  = 108

108 is ascii value of letter l

So we need to convert ascii number to char so we type cast to character type.

str2  = (char) 108

str2  = l

The same process is done until string 1 ends.

5. Print str2 string (converted string )

6. End.