Skip to main content

C++ = How convert lowercase into uppercase and uppercase into lowercase.

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.
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:
#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

Popular posts from this blog

C++ = Code for Counter

Code: #include <iostream> #include <conio.h> using namespace std; int main() {     int count = 0;     char input;     cout << "--------------------------" << endl;     cout << "          Counter         " << endl;     cout << "--------------------------" << endl;     cout << endl;     cout << "Press 1 to counter" << endl;     cout << "Press 0 to quit" << endl;     cout << endl;     cout << "Enter the number continuously : ";     while (true)     {         input = getch();         if (input == '1')         {             count += 1;             cout << "Your counts are " << count << endl;   ...

C++ = Code to check number is a prime or not. If not find the nearest prime number.

Code:  #include <iostream> using namespace std; bool prime(int n) { if (n<=1) { return false; }        for (int i=2;i<=n/2;i++)    {       if (n%i==0)   {   return false;   }    }         return true; } int nearest(int n) { for (int i=n-1;i>=2;i--) { if (prime(i)) { return i; } } return 0; } int main () { int number; cout<<"Enter any number : "; cin>>number; if (prime(number)) { cout<<"The number is prime. "<<endl; }     else     {     cout<<"The entered number is compositive."<<endl;     cout<<"The nearest prime number will be "<<nearest(number)<<endl; } } Result:

C++ = Code for checking any digit palindrome number

Code no: 1 = Using only main function to perform task. #include <iostream> using namespace std; int main() { int number,original,digit,reverse=0; cout<<"Enter any number : "; cin>>number; original=number; while (number!=0) { digit=number%10; reverse=(reverse*10)+digit; number=number/10; } if (original==reverse) { cout<<"This "<<original<<" is Palindrome number."; } else { cout<<"The "<<original<<" is not Palindrome number."; } } Result: Code no: 2 = Using two functions to perform same task. #include <iostream> using namespace std; int isprime(int num) { int digit=0,reverse=0; while (num!=0) { digit=num%10; reverse=(reverse*10)+digit;  num/=10; } return reverse; } int main() { int num; cout<<"Enter any digit to find its sum = "; cin>>num;          int original=num;          if (i...