Skip to content
Related Articles

Related Articles

Check whether the given character is in upper case, lower case or non alphabetic character

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 13 Sep, 2022
Improve Article
Save Article

Given a character, the task is to check whether the given character is in upper case, lower case, or non-alphabetic character 
Examples: 
 

Input: ch = 'A'
Output: A is an UpperCase character

Input: ch = 'a'
Output: a is an LowerCase character

Input: ch = '0'
Output: 0 is not an aplhabetic character

 

Approach: The key to solving this problem lies in the ASCII value of a character. It is the simplest way to find out about a character. This problem is solved with the help of the following detail: 
 

  • Capital letter Alphabets (A-Z) lie in the range 65-91 of the ASCII value
  • Small letter Alphabets (a-z) lie in the range 97-122 of the ASCII value
  • Any other ASCII value is a non-alphabetic character.

Implementation: 
 

C++




// C++ implementation of the above approach
#include<bits/stdc++.h>
using namespace std;
 
void check(char ch)
{
 
    if (ch >= 'A' && ch <= 'Z')
        cout<< ch << " is an UpperCase character\n";
 
    else if (ch >= 'a' && ch <= 'z')
    cout<< ch << " is an LowerCase character\n";
             
 
    else
        cout<< ch << " is not an aplhabetic character\n";
             
}
 
// Driver Code
int main()
{
    char ch;
 
    // Get the character
    ch = 'A';
 
    // Check the character
    check(ch);
 
    // Get the character
    ch = 'a';
 
    // Check the character
    check(ch);
 
    // Get the character
    ch = '0';
 
    // Check the character
    check(ch);
 
    return 0;
}
 
// This code is contributed by Code_Mech

C




// C implementation of the above approach
#include <stdio.h>
 
void check(char ch)
{
 
    if (ch >= 'A' && ch <= 'Z')
        printf("\n%c is an UpperCase character",
               ch);
 
    else if (ch >= 'a' && ch <= 'z')
        printf("\n%c is an LowerCase character",
               ch);
 
    else
        printf("\n%c is not an aplhabetic character",
               ch);
}
 
// Driver Code
int main()
{
    char ch;
 
    // Get the character
    ch = 'A';
 
    // Check the character
    check(ch);
 
    // Get the character
    ch = 'a';
 
    // Check the character
    check(ch);
 
    // Get the character
    ch = '0';
 
    // Check the character
    check(ch);
 
    return 0;
}

Java




// Java implementation of the above approach
 
class GFG
{
 
    static void check(char ch)
    {
     
        if (ch >= 'A' && ch <= 'Z')
            System.out.println("\n" + ch +
                    " is an UpperCase character");
     
        else if (ch >= 'a' && ch <= 'z')
            System.out.println("\n" + ch +
                    " is an LowerCase character" );
     
        else
            System.out.println("\n" + ch +
                    " is not an aplhabetic character" );
    }
 
    // Driver Code
    public static void main(String []args)
    {
        char ch;
     
        // Get the character
        ch = 'A';
     
        // Check the character
        check(ch);
     
        // Get the character
        ch = 'a';
     
        // Check the character
        check(ch);
     
        // Get the character
        ch = '0';
     
        // Check the character
        check(ch);
     
    }
}
 
// This code is contributed by Ryuga

Python3




# Python3 implementation of the above approach
 
def check(ch):
 
    if (ch >= 'A' and ch <= 'Z'):
        print(ch,"is an UpperCase character");
 
    elif (ch >= 'a' and ch <= 'z'):
        print(ch,"is an LowerCase character");
    else:
        print(ch,"is not an aplhabetic character");
 
# Driver Code
 
# Get the character
ch = 'A';
 
# Check the character
check(ch);
 
# Get the character
ch = 'a';
 
# Check the character
check(ch);
 
# Get the character
ch = '0';
 
# Check the character
check(ch);
 
# This code is contributed by mits

C#




// C# implementation of the above approach
using System;
 
class GFG
{
 
    static void check(char ch)
    {
        if (ch >= 'A' && ch <= 'Z')
            Console.WriteLine("\n" + ch +
                    " is an UpperCase character");
        else if (ch >= 'a' && ch <= 'z')
            Console.WriteLine("\n" + ch +
                    " is an LowerCase character" );
        else
            Console.WriteLine("\n" + ch +
                    " is not an aplhabetic character" );
    }
 
    // Driver Code
    public static void Main(String []args)
    {
        char ch;
     
        // Get the character
        ch = 'A';
     
        // Check the character
        check(ch);
     
        // Get the character
        ch = 'a';
     
        // Check the character
        check(ch);
     
        // Get the character
        ch = '0';
     
        // Check the character
        check(ch);
    }
}
 
// This code is contributed by Rajput-JI

PHP




<?php
// PHP implementation of the above approach
 
function check($ch)
{
    if ($ch >= 'A' && $ch <= 'Z')
        print($ch . " is an UpperCase character\n");
 
    else if ($ch >= 'a' && $ch <= 'z')
        print($ch . " is an LowerCase character\n");
    else
        print($ch . " is not an aplhabetic " .
                               "character\n");
}
 
// Driver Code
 
// Get the character
$ch = 'A';
 
// Check the character
check($ch);
 
// Get the character
$ch = 'a';
 
// Check the character
check($ch);
 
// Get the character
$ch = '0';
 
// Check the character
check($ch);
 
// This code is contributed by mits
?>

Javascript




<script>
      // JavaScript implementation of
      // the above approach
 
      function check(ch) {
        if (ch >= "A" && ch <= "Z")
          document.write(ch +
          " is an UpperCase character <br>");
        else if (ch >= "a" && ch <= "z")
          document.write(ch +
          " is an LowerCase character <br>");
        else document.write(ch +
        " is not an aplhabetic character <br>");
      }
 
      // Driver Code
       
      var ch;
      // Get the character
      ch = "A";
       
      // Check the character
      check(ch);
       
      // Get the character
      ch = "a";
       
      // Check the character
      check(ch);
       
      // Get the character
      ch = "0";
       
      // Check the character
      check(ch);
       
</script>

Output

A is an UpperCase character
a is an LowerCase character
0 is not an aplhabetic character

Time Complexity: O(1) as it is doing constant operations
Auxiliary Space: O(1)

Approach using C+++ STL:

islower() – check whether a character is lowercase.

isupper() – check whether a character is uppercase.

Below is the implementation of the above approach.

C++




// C++ code to check if a char is uppercase,
// lowercase or not an aplhabetic character
#include <bits/stdc++.h>
using namespace std;
 
void check(char ch)
{
    if (isupper(ch))
        cout << ch << " is an UpperCase character\n";
 
    else if (islower(ch))
        cout << ch << " is an LowerCase character\n";
 
    else
        cout << ch << " is not an aplhabetic character\n";
}
 
// Driver Code
int main()
{
    char ch;
    ch = 'A';
 
    // Check the character
    check(ch);
 
    // Get the character
    ch = 'a';
 
    // Check the character
    check(ch);
 
    // Get the character
    ch = '0';
 
    // Check the character
    check(ch);
 
    return 0;
    // This code is contributed by Shivesh Kumar Dwivedi
}

Output

A is an UpperCase character
a is an LowerCase character
0 is not an aplhabetic character

Time Complexity: O(1)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!