How to Check if a String Contains Only Lowercase Letters in Java?
A string is a data structure that contains a set of characters. It can be a word or can be a sentence. Also, it can be empty or can have a single letter. A string can contain Uppercase letters, Lowercase letters, or numbers of special characters.
In this article, we will learn how to check if a String contains only lowercase letters in Java.
Approaches to Check Whether a String Contains Only Lowercase Letters
The idea here is very simple. We will go to each letter and check whether it is a lowercase letter or not. We will see two methods for this.
- By Iterating over the String
- Using String.matches function
1. By iterating over the String
- Iterate on the string using a loop from the 0th index to the last index of the string.
- At each iteration check if it is a lowercase letter or not.
- To check it we can use the concept of ascii values. If the ASCII value of the current letter falls between a to z then it is a lowercase letter. Otherwise not.
- If any letter is not a lowercase letter then simply return false as all the string can’t contain lowercase letters now.
- If we iterate through all the characters successfully then return true.
Code Implementation:
Java
// Java program to check if a String contains only lowercase letters import java.util.*; public class LowercaseChecker { public static boolean containsOnlyLowercase(String str) { for ( char c : str.toCharArray()) { // check if the character is not a lowercase letter if (!(c >= 'a' && c <= 'z' )) { return false ; } } // all characters are lowercase letters return true ; } public static void main(String args[]) { // test cases String example1 = "lowercaseonly" ; String example2 = "MixedCase123" ; // check if each example contains only lowercase letters System.out.println( "Example 1 contains only lowercase letters: " + containsOnlyLowercase(example1)); System.out.println( "Example 2 contains only lowercase letters: " + containsOnlyLowercase(example2)); } } |
Example 1 contains only lowercase letters: true Example 2 contains only lowercase letters: false
Explanation of the above Program:
- The
containsOnlyLowercase
method takes a stringstr
as input and iterates over each character in the string. - For each character, it checks if it falls within the lowercase letter range (
'a'
to'z'
). If any character is found that is not a lowercase letter, the method returnsfalse
. - If all characters pass the lowercase check, the method returns
true
. - The
main
method tests thecontainsOnlyLowercase
method with two example strings and prints the results.
Complexity of the above method:
Time complexity: O(N)
Space complexity: O(1)
2. Using String.matches function
In this method, all the things will remain same the change here is instead of checking whether the ASCII value of character lies in between ASCII value of a and z we will use inbuilt function is Java called String.matches. Following is the method for the same.
Code Implementation:
Java
// Java program to check if a String contains // Only lowercase letters using String.matches function import java.util.*; // Driver Class public class LowercaseChecker { // Method to Check if a String // Contains only lowercase letters public static boolean containsOnlyLowercase(String str) { // True if the string contains // Only lowercase letters, otherwise false return str.matches( "[a-z]+" ); } // Main Method to test the containsOnlyLowercase method. public static void main(String args[]) { String example1 = "lowercaseonly" ; String example2 = "MixedCase123" ; System.out.println( "Example 1 contains only lowercase letters: " + containsOnlyLowercase(example1)); System.out.println( "Example 2 contains only lowercase letters: " + containsOnlyLowercase(example2)); } } |
Example 1 contains only lowercase letters: true Example 2 contains only lowercase letters: false
Explanation of the above Program:
- The
containsOnlyLowercase
method takes a stringstr
as input and checks if it contains only lowercase letters using a regular expression[a-z]+
. - The
main
method tests thecontainsOnlyLowercase
method with two examples:example1
contains only lowercase letters, so it printstrue
.example2
contains mixed cases and digits, so it printsfalse
.
Complexity of the above method:
Time complexity: O(N)
Space complexity: O(1)
Contact Us