Check whether the given character is in upper case, lower case or non alphabetic character
Last Updated :
05 Dec, 2023
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
?>
OutputA 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);
OutputA 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');
OutputA is an UpperCase character
a is a LowerCase character
0 is not an alphabetic character
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Basics & Prerequisites
Data Structures
Getting Started with Array Data StructureArray is a collection of items of the same variable type that are stored at contiguous memory locations. It is one of the most popular and simple data structures used in programming. Basic terminologies of ArrayArray Index: In an array, elements are identified by their indexes. Array index starts fr
14 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem