In this example, i’ll show you How to check whether a given character is upper case, lower case, number or special character in C#.
If it is an alphabet and find whether it is an Uppercase alphabet or Lowercase alphabet.
If it is a number display Number else display Symbol.
Input & Output format:
Input consists of one character.
Sample Input :
d
Sample Output :
Lower
Algorithm to check whether a given character is upper case, lower case, number or special character
- Input the character.
- Find the ASCII value of the character.
- If the ASCII value of the character is between 65 and 90, print “Upper”.
- If the ASCII value of the character is between 97 and 122, print “Lower”.
- If the ASCII value of the character is between 48 and 57, print “Number”.
- Else, print “Symbol”.
C# Program to check whether a given character is upper case, lower case, number or special character is given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
static void Main(string[] args) { char ch; Console.Write("Enter a char : "); ch = Console.ReadKey().KeyChar; Console.WriteLine(); if (ch >= 65 && ch <= 90) Console.WriteLine("Upper"); else if (ch >= 97 && ch <= 122) Console.WriteLine("Lower"); else if (ch >= 48 && ch <= 57) Console.WriteLine("Number"); else Console.WriteLine("Symbol"); Console.ReadKey(); } |
Output: