How to check if a character is an alphabet using a condition

Overview

Alphabets range from A-Z and a-z. With this logic, we can specify our condition. And with the condition, we can determine if a certain character, a number, punctuation mark, escape character or sequence, and so on, is an alphabet.

if((character>='a' && character<='z') || (character>='A' && character<='Z'))
Condition to check if a character is an alphabet

The character contains the character we want to check.

Code

Let's look at the code below:

class HelloWorld {
public static void main( String args[] ) {
//create and initialize variables.
char ch1 = '@';
char ch2 = '#';
char ch3 = 'e';
char ch4 = 'L';
// check if alphabets
checkIfAlphabet(ch1);
checkIfAlphabet(ch2);
checkIfAlphabet(ch3);
checkIfAlphabet(ch4);
}
static void checkIfAlphabet(char ch){
//condition for checking characters.
if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z'))
{
System.out.print(ch + " is an Alphabet\n");
}
else
{
System.out.print(ch + " is not an Alphabet\n");
}
}
}

Explanation

  • Lines 4 to 7: We create and initialize some character variables.
  • Lines 10 to 13: We call checkIfAlphabet() to check if the characters we pass are alphabets or not.
  • Lines 16 to 27: We create a checkIfAlphabet() function that takes a character and checks if it is an alphabet using the specified condition.

Free Resources