Character Type Checker in C#
This C# program prompts the user to input a single character and checks whether it is an alphabet letter, a digit, or a special character. The program uses character classification methods to identify the type of character and outputs the result, demonstrating conditional logic and built-in C# functions for handling character data.
On this page
Source Code 💻
using System;
class Program
{
static void Main()
{
// Prompt user to enter a character
Console.Write("Enter a character: ");
char inputChar = Console.ReadKey().KeyChar;
Console.WriteLine(); // For new line after input
// Check if the character is an alphabet, digit, or special character
if (Char.IsLetter(inputChar))
{
Console.WriteLine($"{inputChar} is an alphabet.");
}
else if (Char.IsDigit(inputChar))
{
Console.WriteLine($"{inputChar} is a digit.");
}
else
{
Console.WriteLine($"{inputChar} is a special character.");
}
}
}
Explanation
- The program prompts the user to input a character.
- It uses the Char.IsLetter() method to check if the character is an alphabet letter (a-z or A-Z).
- The Char.IsDigit() method is used to check if the character is a digit (0-9).
- If the character is neither a letter nor a digit, it is classified as a special character.
- The result is printed accordingly.
Example
-
Input: a
-
Output: a is an alphabet.
-
Input: 5
-
Output: 5 is a digit.
-
Input: @
-
Output: @ is a special character.
🔚 end of document 🔚