Given a string, the task is to check whether a string contains only alphabets or not using ASCII values in JAVA.
Examples:
Input : GeeksforGeeks Output : True Input : Geeks4Geeks Output : False Input : null Output : False
In this article, the characters of the string are checked one by one using their ASCII values.
Algorithm:
- Get the string
- Match the string:
- Check if the string is empty or not. If empty, return false
- Check if the string is null or not. If null, return false.
- If the string is neither empty nor null, then check the string characters one by one for alphabet using ASCII values.
- Return true if matched
Pseudocode:
public static boolean isStringOnlyAlphabet(String str) { if (str == null || str.equals("")) { return false; } for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if ((!(ch >= 'A' && ch <= 'Z')) && (!(ch >= 'a' && ch <= 'z'))) { return false; } } return true; } |
Program: Checking for String containing only Alphabets
// Java program to check if String contains only Alphabets // using ASCII values class GFG { // Function to check String for only Alphabets public static boolean isStringOnlyAlphabet(String str) { if (str == null || str.equals("")) { return false; } for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if ((!(ch >= 'A' && ch <= 'Z')) && (!(ch >= 'a' && ch <= 'z'))) { return false; } } return true; } // Main method public static void main(String[] args) { // Checking for True case System.out.println("Test Case 1:"); String str1 = "GeeksforGeeks"; System.out.println("Input: " + str1); System.out.println("Output: " + isStringOnlyAlphabet(str1)); // Checking for String with numeric characters System.out.println("\nTest Case 2:"); String str2 = "Geeks4Geeks"; System.out.println("Input: " + str2); System.out.println("Output: " + isStringOnlyAlphabet(str2)); // Checking for null String System.out.println("\nTest Case 3:"); String str3 = null; System.out.println("Input: " + str3); System.out.println("Output: " + isStringOnlyAlphabet(str3)); // Checking for empty String System.out.println("\nTest Case 4:"); String str4 = ""; System.out.println("Input: " + str4); System.out.println("Output: " + isStringOnlyAlphabet(str4)); } } |
Test Case 1: Input: GeeksforGeeks Output: true Test Case 2: Input: Geeks4Geeks Output: false Test Case 3: Input: null Output: false Test Case 4: Input: Output: false
Check if a string contains only alphabets in Java using Lambda expression
Check if a string contains only alphabets in Java using Regex
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Check if a string contains only alphabets in Java using Lambda expression
- Check if a string contains only alphabets in Java using Regex
- Check if a string contains only alphabets in Java
- Count of alphabets having ASCII value less than and greater than k
- Count and Print the alphabets having ASCII value in the range [l, r]
- Count and Print the alphabets having ASCII value not in the range [l, r]
- Sub-string that contains all lowercase alphabets after performing the given operation
- Program to check if a String in Java contains only whitespaces
- How to check if string contains only digits in Java
- Average of ASCII values of characters of a given string
- Program to find the product of ASCII values of characters in a string
- Count characters in a string whose ASCII values are prime
- Program to find the XOR of ASCII values of characters in a string
- Split a given string into substrings of length K with equal sum of ASCII values
- Convert a string to hexadecimal ASCII values
- Check if a string contains uppercase, lowercase, special characters and numeric values
- Map function and Dictionary in Python to sum ASCII values
- Minimize ASCII values sum after removing all occurrences of one character
- Count the number of words having sum of ASCII values less than and greater than k
- Print each word in a sentence with their corresponding average of ASCII values
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

