Exercise
Function Isalphabetic
Objective
Write a C# function that tells if a character is alphabetic (A through Z) or not. It should be used like this:
if (IsAlphabetic ("a"))
System.Console.WriteLine ("It is an alphabetic character");
(Note: do not worry about accents and ñ)
Write Your C# Exercise
C# Exercise Example
// Import the System namespace to use basic classes like Console
using System;
class Program
{
// Main method to drive the program
public static void Main()
{
// Test the IsAlphabetic function
if (IsAlphabetic("a"))
{
Console.WriteLine("It is an alphabetic character");
}
else
{
Console.WriteLine("It is not an alphabetic character");
}
if (IsAlphabetic("1"))
{
Console.WriteLine("It is an alphabetic character");
}
else
{
Console.WriteLine("It is not an alphabetic character");
}
}
// Function to check if a character is alphabetic
public static bool IsAlphabetic(string str)
{
// Check if the string contains exactly one character and it is a letter
return str.Length == 1 && char.IsLetter(str[0]);
}
}