Thursday 18 October 2018

Conversion of lowercase alphabet to uppercase or vice versa in C++

ASCII value/code


A computer is a digital device so it converts data & instructions into binary bits before performing any operation on it. The binary bits are generated by the encoder according to the character encoding standard. The binary bits may vary depending on the encoding standard used. The two most commonly used character encoding standards are ASCII and EBCDIC. Both the encoding standard have their own unique binary code for each character.

ASCII stands for American Standard Code for Information Interchange and is the most popular character encoding standard used for electronic communication (i.e. information interchange or transfer in electronics). In this standard, 128 characters are numerically encoded with a unique 7-bit binary combination known as 7-bit ASCII value. All the operations and processing within a digital system are performed on these binary combinations. But the decimal equivalent of the corresponding  7-bit binary codes is generally considered as ASCII values. Some of the characters with ASCII value and 7-bit ASCII value are given below.   

Character                    ASCII value                    7-bit ASCII
a                                   97                                    1100001
b                                   98                                    1100010
y                                   121                                  1111001
z                                   122                                  1111010
A                                   65                                   1000001
B                                   66                                   1000010
Y                                   88                                   1011001
Z                                   89                                   1011010
7                                   55                                    0110111
LF(Line feed)               10                                    0001010
                                                                                           FULL TABLE

From the table, we can observe that the ASCII value of any lowercase alphabet and the same uppercase alphabet differ by 32.
i.e.
               ASCII value of any uppercase alphabet + 32 = ASCII value of same lowercase alphabet

Example:
               ASCII value of 'B' + 32 = ASCII value of 'b'
                                       66 + 32 = 98


Write a program in C++ to input an alphabet from the user. If the entered character is in uppercase then convert it into lowercase or vice versa.


In this program, we input an alphabet from the user. We check the form(uppercase or lowercase) of the alphabet by comparing its ASCII value. If the entered alphabet is in uppercase we obtain the lowercase by adding 32 to the ASCII value. And if the entered alphabet is in lowercase we obtain the uppercase by subtracting 32 to the ASCII value. Then we display the alternative form of the alphabet.


PROGRAM


#include<iostream.h>
#include<conio.h>
void main()
{
char a;
clrscr();
cout<<"Enter an alphabet either in uppercase or lowercase=";
cin>>a;
if((a>=65)&&(a<=90))
{
a=a+32;
cout<<"\nLowercase="<<a;
}
else if((a>=97)&&(a<=122))
{
a=a-32;
cout<<"\nUppercase="<<a;
}
else
cout<<"\nInvalid input";
getch();
}


OUTPUT

Conversion of cases using ASCII value
PROGRAM TO FIND ALTERNATE CASE OF AN ALPHABET












EXPLANATION

In the program, 'r' is the entered alphabet whose ASCII value is 114. The alphabet is in lowercase so the second condition of the if....else if statement is true. The following expression is executed.

ASCII value of 'r' - 32 = 114-32
                                      = 82 (which is the ASCII value of 'R')

Finally, 'R' which is in uppercase was printed on the output screen.







No comments:

Post a Comment