UPPER or lower Case Character || Conditional Statement || C++ || Turbo C++


In this, we are going to write a simple program to check if a alphabet character input by user is in UPPER or lower case or he has given an invalid character in C++.

We first check if the input character is an alphabet or not. If it's an alphabet then if the ascii value of character is between 65 and 90, then it must be an upper case character otherwise a lower case character whose ascii is 97 to 122.

The code given below can be used for TURBO C++ Compiler:-
// check character is UPPER OR lower case character ?
#include<iostream.h>
#include<conio.h>

void main()
{
    clrscr();
    char ch;
    cout << "Enter character to find if it's UPPER / lower case\n";
    cin >> ch;
    // to check if character entered is an alphabet
    // with help of ascii values of alphabets
    if ((int(ch) >= 97 && int(ch) <= 122) || (int(ch) >= 65 && int(ch) <= 90))
    {
        // if character is actually alphabet
        if ((int(ch) >= 97 && int(ch) <= 122))
        {
            cout << "you entered lower case "<< ch;
        }
        else 
        {
            cout << "you entered UPPER case "<< ch;
        }
    }
    else
    {
        cout << "INVALID character";
    }
    getch();
}
  
    

The code given below can be used for g++/gcc Compiler:-


// check character is UPPER OR lower case character ?
#include <iostream>
using namespace std;
int main()
{
    char ch;
    cout << "Enter character to find if it's UPPER / lower case\n";
    cin >> ch;
    // to check if character entered is an alphabet
    // with help of ascii values of alphabets
    if ((int(ch) >= 97 && int(ch) <= 122) || (int(ch) >= 65 && int(ch) <= 90))
    {
        // if character is actually alphabet
        if ((int(ch) >= 97 && int(ch) <= 122))
        {
            cout << "you entered lower case "<< ch;
        }
        else 
        {
            cout << "you entered UPPER case "<< ch;
        }
    }
    else
    {
        cout << "INVALID character";
    }
    return 0;
}

  
    

#ENJOYCODING
 

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post