The Wayback Machine - https://web.archive.org/web/20240718074441/https://www.geeksforgeeks.org/check-whether-the-given-character-is-in-upper-case-lower-case-or-non-alphabetic-character/
Open In App

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

Last Updated : 05 Dec, 2023
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

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 alphabetic 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 alphabetic 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 alphabetic 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 alphabetic 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 alphabetic 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 alphabetic 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


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 alphabetic 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>


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 alphabetic " .
                               "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
?>


Output

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




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

Check whether the given character is in upper case, lower case, or non-alphabetic character using the inbuilt library: 

C++




// C++ code to check if a char is uppercase,
// lowercase or not an alphabetic 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 a lowerCase character\n";
 
    else
        cout << ch << " is not an alphabetic 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
}


Java




import java.util.*;
 
class Main {
 
    public static void check(char ch)
    {
        if (Character.isUpperCase(ch))
            System.out.println(
                ch + " is an upperCase character");
        else if (Character.isLowerCase(ch))
            System.out.println(
                ch + " is a lowerCase character");
        else
            System.out.println(
                ch + " is not an alphabetic character");
    }
 
    public static void main(String[] args)
    {
        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);
    }
}


Python3




def check(ch):
    if ch.isupper():
        print(ch, "is an upperCase character")
    elif ch.islower():
        print(ch, "is a lowerCase character")
    else:
        print(ch, "is not an alphabetic character")
 
# Driver Code
if __name__ == '__main__':
    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)


C#




using System;
 
class Program
{
    static void check(char ch)
    {
        if (Char.IsUpper(ch))
            Console.WriteLine("{0} is an upperCase character", ch);
 
        else if (Char.IsLower(ch))
            Console.WriteLine("{0} is a lowerCase character", ch);
 
        else
            Console.WriteLine("{0} is not an alphabetic character", ch);
    }
 
    static void Main(string[] args)
    {
        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);
 
        // Pause the console
        Console.ReadLine();
    }
}


Javascript




function check(ch) {
  if (ch.match(/[A-Z]/)) {
    console.log(ch + " is an upperCase character");
  } else if (ch.match(/[a-z]/)) {
    console.log(ch + " is a lowerCase character");
  } else {
    console.log(ch + " is not an alphabetic character");
  }
}
 
// Driver Code
let 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);


Output

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




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

Approach :

This implementation uses a switch statement to check the value of the character. If it is an uppercase letter, it will print that it is an uppercase letter. If it is a lowercase letter, it will print that it is a lowercase letter. Otherwise, it will print that it is not an alphabetic character.

C++




#include <iostream>
using namespace std;
 
void check(char ch)
{
    switch(ch)
    {
        case 'A':
        case 'B':
        case 'C':
        case 'D':
        case 'E':
        case 'F':
        case 'G':
        case 'H':
        case 'I':
        case 'J':
        case 'K':
        case 'L':
        case 'M':
        case 'N':
        case 'O':
        case 'P':
        case 'Q':
        case 'R':
        case 'S':
        case 'T':
        case 'U':
        case 'V':
        case 'W':
        case 'X':
        case 'Y':
        case 'Z':
            cout << ch << " is an UpperCase character\n";
            break;
         
        case 'a':
        case 'b':
        case 'c':
        case 'd':
        case 'e':
        case 'f':
        case 'g':
        case 'h':
        case 'i':
        case 'j':
        case 'k':
        case 'l':
        case 'm':
        case 'n':
        case 'o':
        case 'p':
        case 'q':
        case 'r':
        case 's':
        case 't':
        case 'u':
        case 'v':
        case 'w':
        case 'x':
        case 'y':
        case 'z':
            cout << ch << " is a LowerCase character\n";
            break;
             
        default:
            cout << ch << " is not an alphabetic character\n";
            break;
    }
}
 
int main()
{
    char ch;
     
    ch = 'A';
    check(ch);
     
    ch = 'a';
    check(ch);
     
    ch = '0';
    check(ch);
     
    return 0;
}


Java




public class CharacterCheck {
    public static void check(char ch) {
        switch (ch) {
            case 'A':
            case 'B':
            case 'C':
            case 'D':
            case 'E':
            case 'F':
            case 'G':
            case 'H':
            case 'I':
            case 'J':
            case 'K':
            case 'L':
            case 'M':
            case 'N':
            case 'O':
            case 'P':
            case 'Q':
            case 'R':
            case 'S':
            case 'T':
            case 'U':
            case 'V':
            case 'W':
            case 'X':
            case 'Y':
            case 'Z':
                System.out.println(ch + " is an UpperCase character");
                break;
 
            case 'a':
            case 'b':
            case 'c':
            case 'd':
            case 'e':
            case 'f':
            case 'g':
            case 'h':
            case 'i':
            case 'j':
            case 'k':
            case 'l':
            case 'm':
            case 'n':
            case 'o':
            case 'p':
            case 'q':
            case 'r':
            case 's':
            case 't':
            case 'u':
            case 'v':
            case 'w':
            case 'x':
            case 'y':
            case 'z':
                System.out.println(ch + " is a LowerCase character");
                break;
 
            default:
                System.out.println(ch + " is not an alphabetic character");
                break;
        }
    }
 
    public static void main(String[] args) {
        char ch;
 
        ch = 'A';
        check(ch);
 
        ch = 'a';
        check(ch);
 
        ch = '0';
        check(ch);
    }
}


Python




def check(ch):
    # Switch case equivalent using if-elif-else
    if ch.isupper():
        print(ch, "is an UpperCase character")
    elif ch.islower():
        print(ch, "is a LowerCase character")
    else:
        print(ch, "is not an alphabetic character")
 
 
# Driver Code
ch = 'A'
check(ch)
 
ch = 'a'
check(ch)
 
ch = '0'
check(ch)


C#




using System;
 
class Program
{
    // Function to check the type of character
    static void CheckCharType(char ch)
    {
        switch (ch)
        {
            // Uppercase letters
            case 'A':
            case 'B':
            case 'C':
            case 'D':
            case 'E':
            case 'F':
            case 'G':
            case 'H':
            case 'I':
            case 'J':
            case 'K':
            case 'L':
            case 'M':
            case 'N':
            case 'O':
            case 'P':
            case 'Q':
            case 'R':
            case 'S':
            case 'T':
            case 'U':
            case 'V':
            case 'W':
            case 'X':
            case 'Y':
            case 'Z':
                Console.WriteLine(ch + " is an Uppercase character");
                break;
 
            // Lowercase letters
            case 'a':
            case 'b':
            case 'c':
            case 'd':
            case 'e':
            case 'f':
            case 'g':
            case 'h':
            case 'i':
            case 'j':
            case 'k':
            case 'l':
            case 'm':
            case 'n':
            case 'o':
            case 'p':
            case 'q':
            case 'r':
            case 's':
            case 't':
            case 'u':
            case 'v':
            case 'w':
            case 'x':
            case 'y':
            case 'z':
                Console.WriteLine(ch + " is a Lowercase character");
                break;
 
            // Not an alphabetic character
            default:
                Console.WriteLine(ch + " is not an alphabetic character");
                break;
        }
    }
 
    static void Main()
    {
        char ch;
 
        ch = 'A';
        CheckCharType(ch);
 
        ch = 'a';
        CheckCharType(ch);
 
        ch = '0';
        CheckCharType(ch);
 
        // Pause the console before exiting
        Console.ReadLine();
    }
}


Javascript




function checkCharType(ch) {
    switch (ch) {
        // Uppercase letters
        case 'A':
        case 'B':
        case 'C':
        case 'D':
        case 'E':
        case 'F':
        case 'G':
        case 'H':
        case 'I':
        case 'J':
        case 'K':
        case 'L':
        case 'M':
        case 'N':
        case 'O':
        case 'P':
        case 'Q':
        case 'R':
        case 'S':
        case 'T':
        case 'U':
        case 'V':
        case 'W':
        case 'X':
        case 'Y':
        case 'Z':
            console.log(ch + " is an Uppercase character");
            break;
 
        // Lowercase letters
        case 'a':
        case 'b':
        case 'c':
        case 'd':
        case 'e':
        case 'f':
        case 'g':
        case 'h':
        case 'i':
        case 'j':
        case 'k':
        case 'l':
        case 'm':
        case 'n':
        case 'o':
        case 'p':
        case 'q':
        case 'r':
        case 's':
        case 't':
        case 'u':
        case 'v':
        case 'w':
        case 'x':
        case 'y':
        case 'z':
            console.log(ch + " is a Lowercase character");
            break;
 
        // Not an alphabetic character
        default:
            console.log(ch + " is not an alphabetic character");
            break;
    }
}
 
// Test cases with sample characters
checkCharType('A');
checkCharType('a');
checkCharType('0');


Output

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





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



Previous Article
Next Article

Similar Reads

Lower case to upper case - An interesting fact
Problem: Given a string containing only lowercase letters, generate a string with the same letters, but in uppercase. Input : GeeksForGeeks Output : GEEKSFORGEEKS Recommended PracticeLower case to upper caseTry It! The first method that comes to our mind is C/C++ Code // C++ program to convert a string to uppercase #include &lt;iostream&gt; using n
7 min read
Case conversion (Lower to Upper and Vice Versa) of a string using BitWise operators in C/C++
Given a string, write a function that converts it either from lower to upper case or from upper to lower case using the bitwise operators &amp;(AND), |(OR), ~(NOT) in place and returns the string. Many of us know that Bitwise manipulations are faster than performing arithmetic operations for a compiler as the data is stored in binary form 0's and 1
5 min read
Swap upper and lower triangular halves of a given Matrix
Given a square matrix mat[][] of dimensions N * N, the task is to print the matrix that can be obtained after swapping the laterally inverted images of the upper and lower triangular halves of a given matrix. Consider the matrix mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}The lateral image of the lower triangular half of the matrix 47 8The lateral i
13 min read
Convert vowels into upper case character in a given string
Given string str, the task is to convert all the vowels present in the given string to uppercase characters in the given string. Examples: Input: str = “GeeksforGeeks”Output: GEEksfOrGEEksExplanation:Vowels present in the given string are: {'e', 'o'}Replace 'e' to 'E' and 'o' to 'O'.Therefore, the required output is GEEksfOrGEEks. Input: str = “eut
7 min read
Python String Methods | Set 1 (find, rfind, startwith, endwith, islower, isupper, lower, upper, swapcase &amp; title)
Some of the string basics have been covered in the below articles Strings Part-1 Strings Part-2 The important string methods will be discussed in this article1. find("string", beg, end) :- This function is used to find the position of the substring within a string.It takes 3 arguments, substring , starting index( by default 0) and ending index( by
4 min read
C Program to Convert a Char From Upper to Lower and Vice Versa in a Single Line Efficiently
Here we will build a program to showcase an efficient way to convert a char from upper to lower and vice versa in C in a single line. Below is one example which explains how bitwise operators help in efficient programming and also convert the given alphabet from one case to another. Example: C/C++ Code // C program to demonstrate converting of // c
1 min read
POTD Solutions | 07 Nov’ 23 | Sum of upper and lower triangles
View all POTD Solutions Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Matrix but will also help you build up problem-solving skills. We recommend you to try this problem on ou
5 min read
Program to swap upper diagonal elements with lower diagonal elements of matrix.
Given a square matrix, swap upper diagonal elements of matrix with lower diagonal elements of matrix. Examples : Input: 2 3 5 6 4 5 7 9 8 6 4 9 1 3 5 6 Output: 2 4 8 1 3 5 6 3 5 7 4 5 6 9 9 6 Input: 1 2 3 4 5 6 7 8 9 Output: 1 4 7 2 5 8 3 6 9Recommended PracticeSwapping TrianglesTry It! Below is the implementation of above idea : C/C++ Code // CPP
7 min read
Alternate Lower Upper String Sort
Given a string containing lowercase and uppercase letters. Sort it in such a manner that the uppercase and lowercase letters come in an alternate manner but in a sorted way. Examples: Input : bAwutndekWEdkd Output :AbEdWddekkntuw Explanation: Here we can see that letter ‘A’, ’E’, ’W’ are sorted as well as letters “b, d, d, d, e, k, k, n, t, u, w” a
6 min read
Program to print Lower triangular and Upper triangular matrix of an array
Prerequisite - Multidimensional Arrays in C / C++Given a two dimensional array, Write a program to print lower triangular matrix and upper triangular matrix. Lower triangular matrix is a matrix which contains elements below principal diagonal including principal diagonal elements and rest of the elements are 0.Upper triangular matrix is a matrix wh
9 min read
three90RightbarBannerImg