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++
#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" ;
}
int main()
{
char ch;
ch = 'A' ;
check(ch);
ch = 'a' ;
check(ch);
ch = '0' ;
check(ch);
return 0;
}
|
C
#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);
}
int main()
{
char ch;
ch = 'A' ;
check(ch);
ch = 'a' ;
check(ch);
ch = '0' ;
check(ch);
return 0;
}
|
Java
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" );
}
public static void main(String []args)
{
char ch;
ch = 'A' ;
check(ch);
ch = 'a' ;
check(ch);
ch = '0' ;
check(ch);
}
}
|
Python3
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" );
ch = 'A' ;
check(ch);
ch = 'a' ;
check(ch);
ch = '0' ;
check(ch);
|
C#
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" );
}
public static void Main(String []args)
{
char ch;
ch = 'A' ;
check(ch);
ch = 'a' ;
check(ch);
ch = '0' ;
check(ch);
}
}
|
PHP
<?php
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" );
}
$ch = 'A' ;
check( $ch );
$ch = 'a' ;
check( $ch );
$ch = '0' ;
check( $ch );
?>
|
Javascript
<script>
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>" );
}
var ch;
ch = "A" ;
check(ch);
ch = "a" ;
check(ch);
ch = "0" ;
check(ch);
</script>
|
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++
#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" ;
}
int main()
{
char ch;
ch = 'A' ;
check(ch);
ch = 'a' ;
check(ch);
ch = '0' ;
check(ch);
return 0;
}
|
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(ch);
ch = 'a' ;
check(ch);
ch = '0' ;
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" )
if __name__ = = '__main__' :
ch = 'A'
check(ch)
ch = 'a'
check(ch)
ch = '0'
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(ch);
ch = 'a' ;
check(ch);
ch = '0' ;
check(ch);
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" );
}
}
let ch;
ch = 'A' ;
check(ch);
ch = 'a' ;
check(ch);
ch = '0' ;
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;
}
|
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)
Please Login to comment...