
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If Character Is an Alphabet in Java
For a character "ch", write a Java program to verify whether it lies between a and z (both small and capital). If it does it is an alphabet otherwise, not.
Alphabets are the set of letters written to represent particular sounds in a spoken language like English.
Example Scenario 1:
Input: character = 'e'; Output: Yes given character is an alphabet
Example Scenario 2:
Input: character = '4'; Output: No given character is not an alphabet
Using ASCII Values
The term ASCII stands for American Standard Code for Information Interchange. Every English letters has an ASCII value associated with it. For alphabetic letters, these values range from 65 to 90 for uppercase letters and 97 to 122 for lowercase letters.
Using if-else statement, we check whether the ASCII value comes within these ranges. If it does, the character is considered an alphabet letter.
Example
In this example, we use the ASCII values to find whether the given character is an alphabet or not.
public class AlphabetOrNot { public static void main(String args[]){ char ch = 'G'; System.out.println("Given character:: " + ch); // checking character is alphabet or not if(((ch >= 'A' && ch <= 'Z')||ch >= 'a' && ch <= 'z') ){ System.out.println("Given character is an alphabet"); }else{ System.out.println("Given character is not an alphabet"); } } }
When you run the above code, you will get the following output ?
Given character:: G Given character is an alphabet
Using isLetter() Method
The isLetter() method of the Character class is used to determine if a specific character is a letter or not. This method accepts a character as an argument and returns TRUE if the character argument is a letter, otherwise FALSE.
Example
The following Java program shows how to check whether a given character is alphabet or not.
public class AlphabetOrNot { public static void main(String args[]) { char ch = 'n'; System.out.println("Given character:: " + ch); // using isLetter() method if (Character.isLetter(ch)) { System.out.println("Given character is an alphabet"); } else { System.out.println("Given character is not an alphabet"); } } }
On executing, this program gives the below result ?
Given character:: n Given character is an alphabet
Using Regular Expressions
A regular expression in Java is a special sequence of characters that helps you find other character or sets of strings, using a specialized syntax held in a pattern.
We pass this pattern as a parameter value to matches() method of Java String class which will return TRUE if, and only if, this pattern matches the given regular expression.
Example
In this Java program, we use the regular expression to verify whether the given character is alphabet or not.
public class AlphabetOrNot { public static void main(String args[]) { char ch = '4'; System.out.println("Given character:: " + ch); // using regular expression if (String.valueOf(ch).matches("[A-Za-z]")) { System.out.println("Given character is an alphabet"); } else { System.out.println("Given character is not an alphabet"); } } }
On running this code, it will display following result ?
Given character:: 4 Given character is not an alphabet