First you need to know that what are the ASCII codes for the alphabets.
As you can see there, that the ASCII code for A is 65 and for a is 97, whose difference is 32 or we can say that the condition for it is ('a'-'A') which is also equal to 32 i.e (97-65=32).
We will be trying to do a brainstroming. Now, see we know that Uppercase aplhabets have lower value and Lowercase aplhabets have higher value but there is difference of 32 between every corresponding element like z to Z.
We will be trying to do a brainstroming. Now, see we know that Uppercase aplhabets have lower value and Lowercase aplhabets have higher value but there is difference of 32 between every corresponding element like z to Z.
If we want to go lowercase to uppercase we need to subtract 32 from the given character.
And If we want to go from uppercase to lowercase we need to to add 32 to that given character.
Note: It is our human preception that how 32 will be subtracted or added from a character but it is not case with computer, computer will consider its ASCII code.
So the below code is making condition of the below logic:
Code:
So the below code is making condition of the below logic:
Code:
#include <iostream>
using namespace std;
int main()
{
char character;
cout<<"Enter any character : ";
cin>>character;
if (character>='a' && character<='z')
{
char newch;
newch=character-(32);
cout<<"The uppercase is "<<newch;
}
else if (character>='A' && character<='Z')
{
char newch;
newch=character+(32);
cout<<"The lowercase is "<<newch;
}
}
Result:
Comments
Post a Comment