C++ Program to convert lower case to upper case


February 9, 2023, Learn eTutorial
1537

In this C++ program, you will learn to convert lower-case characters to upper-case characters.

Uppercase Character

An uppercase character is an alphabet character written in capital letters. ASCII values of all 26 uppercase alphabet characters are 65-90. Where 65 is the ASCII value of A, 66 is the ASCII value of B, and so on up to, 90 is the ASCII value of Z

Lowercase Character

A lowercase is an alphabet written in small case letters. ASCII values of all 26 lowercase alphabets are 97-122. That is, a = 97, b = 98, c = 99, and so on.

How to convert lowercase characters to uppercase?

Since the ASCII value of a (capital letter) is 97 and the ASCII value of A (small letter) is 65. The difference between them is 32. Therefore, on subtracting 32 from the ASCII value of a small letter say a, then we'll get the ASCII value of its equivalent letter in uppercase. That is, 97 (ASCII value of a) - 32 = 65 (ASCII value of A)

How to write a C++ program to convert lowercase characters to uppercase?

The program asks the user to enter a lowercase character. Read the entered character into the character type variable lowerch. Initialize the ASCII value of the entered character to the integer type variable asciivalue.

asciivalue = lowerch;

Then subtract 32 from the ASCII value and update the value of the variable asciivalue. The updated value is the ASCII value of the upper case of the entered character.

asciivalue = asciivalue - 32;

Get the updated value and print the character corresponding to the value on the screen

Algorithm

Step 1: Call the header file iostream.

Step 2: Use the namespace std.

Step 3: Open the main function; int main().

Step 4: Declare two-character type variables upperch and lowerch and integer type variable asciivalue;

Step 5: Ask the user to enter an uppercase character;

Step 6: Read the character into the variable lowerch;

Step 7: Get the ASCII value of the character to the variable asciivalue;

Step 8: subtract 32 from the asciivalue;

Step 9: Convert  the updated value of asciivalue to the character and get it into the  variable upperch;

Step 10: Print the character in upperch;

Step 11:Exit 

C++ Source Code

                                          #include<iostream>
using namespace std;
int main()
{
    char upperch, lowerch;
    int asciivalue;
    cout<<"Enter a lowercase Character : ";
    cin>>lowerch;
    asciivalue =lowerch;
    asciivalue = asciivalue-32;
    upperch = asciivalue;
    cout<<"\nIts uppercase: "<<upperch;
    cout<<endl;
    return 0;
}
                                      

OUTPUT

Enter a lowercase Character : g
Its uppercase: G