Check if a string contains only alphabets in Java
Given a string, the task is to write a Java program to check whether the string contains only alphabets or not. If so, then print true, otherwise false.
Explanation: The given string contains only alphabets so the output is true.
Explanation: The given string contains alphabets and numbers so the output is false.
Explanation: The given string is empty so the output is false.
Method 4: This approach uses the methods of Character class in Java
- The idea is to iterate over each character of the string and check whether the specified character is a letter or not using Character.isLetter() method.
- If the character is a letter then continue, else return false.
- If we are able to iterate over the whole string, then return true.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.