Given a character, the task is to check whether the given character is in upper case, lower case, or non-alphabetic character
Examples:
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:
#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"; } int main() { char ch; ch = 'A'; check(ch); ch = 'a'; check(ch); ch = '0'; check(ch); return 0; } |
#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); } int main() { char ch; ch = 'A'; check(ch); ch = 'a'; check(ch); ch = '0'; check(ch); return 0; } |
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" ); } public static void main(String []args) { char ch; ch = 'A'; check(ch); ch = 'a'; check(ch); ch = '0'; check(ch); } } |
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"); ch = 'A'; check(ch); ch = 'a'; check(ch); ch = '0'; check(ch); |
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" ); } public static void Main(String []args) { char ch; ch = 'A'; check(ch); ch = 'a'; check(ch); ch = '0'; check(ch); } } |
<?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 aplhabetic " . "character\n"); } $ch = 'A'; check($ch); $ch = 'a'; check($ch); $ch = '0'; check($ch); ?> |
<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 aplhabetic character <br>"); } var ch; ch = "A"; check(ch); ch = "a"; check(ch); ch = "0"; check(ch); </script> |
OutputA 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)
islower() – check whether a character is lowercase.
isupper() – check whether a character is uppercase.
Below is the implementation of the above approach.
#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"; } int main() { char ch; ch = 'A'; check(ch); ch = 'a'; check(ch); ch = '0'; check(ch); return 0; } |
OutputA is an UpperCase character a is an LowerCase character 0 is not an aplhabetic character
Time Complexity: O(1)
Auxiliary Space: O(1)