How to Check if a String Contains Only Lowercase Letters in Java?
Last Updated :
15 Feb, 2024
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
import java.util.*;
public class LowercaseChecker
{
public static boolean containsOnlyLowercase(String str)
{
for ( char c : str.toCharArray())
{
if (!(c >= 'a' && c <= 'z' ))
{
return false ;
}
}
return true ;
}
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));
}
}
|
Output
Example 1 contains only lowercase letters: true
Example 2 contains only lowercase letters: false
Explanation of the above Program:
- The
containsOnlyLowercase
method takes a string str
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 returns false
.
- If all characters pass the lowercase check, the method returns
true
.
- The
main
method tests the containsOnlyLowercase
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
import java.util.*;
public class LowercaseChecker {
public static boolean containsOnlyLowercase(String str)
{
return str.matches( "[a-z]+" );
}
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));
}
}
|
Output
Example 1 contains only lowercase letters: true
Example 2 contains only lowercase letters: false
Explanation of the above Program:
- The
containsOnlyLowercase
method takes a string str
as input and checks if it contains only lowercase letters using a regular expression [a-z]+
.
- The
main
method tests the containsOnlyLowercase
method with two examples:
example1
contains only lowercase letters, so it prints true
.
example2
contains mixed cases and digits, so it prints false
.
Complexity of the above method:
Time complexity: O(N)
Space complexity: O(1)
Share your thoughts in the comments
Please Login to comment...