In this article, we will understand how to check whether a character is alphabet or not. This is accomplished by checking if the given character lies in between the alphabets a to z or A to Z.
Below is a demonstration of the same −
Input
Suppose our input is −
Enter a character: H
Output
The desired output would be −
The character H is an alphabet
Algorithm
Step 1 - START Step 2 - Declare a character value namely my_input Step 3 - Read the required values from the user/ define the values Step 4 - Using an if-else condition, check if the input value lies in between ‘a’ and ‘z’ or ‘A’ and ‘Z’ using comparison operators ‘>=’ and ‘<=’ . If true, its an alphabet, else its not an alphabet/ Step 5 - Display the result Step 6 - Stop
Example 1
Here, the input is being entered by the user based on a prompt. You can try this example live in our coding ground tool .
import java.util.Scanner; public class Alphabet { public static void main(String[] args) { char my_input; System.out.println("Required packages have been imported"); Scanner my_scanner = new Scanner(System.in); System.out.println("A reader object has been defined "); System.out.print("Enter the character : "); my_input = my_scanner.next().charAt(0); if( (my_input >= 'a' && my_input <= 'z') || (my_input >= 'A' && my_input <= 'Z')) System.out.println("The character " +my_input + " is an alphabet"); else System.out.println("The character " +my_input + " is not an alphabet"); } }
Output
Required packages have been imported A reader object has been defined Enter the character : H The character H is an alphabet
Example 2
Here, the integer has been previously defined, and its value is accessed and displayed on the console.
public class Alphabet { public static void main(String[] args) { char my_input; my_input = 'H'; System.out.println("The Alphabet is defined as " +my_input); if( (my_input >= 'a' && my_input <= 'z') || (my_input >= 'A' && my_input <= 'Z')) System.out.println("The character " +my_input + " is an alphabet."); else System.out.println("The character " +my_input + " is not an alphabet."); } }
Output
The Alphabet is defined as H The character H is an alphabet.