In Java, converting an uppercase string to a lowercase string is a common and simple task. This can be done using Java’s built-in toLowerCase()
method or manually through loops and character manipulation.
For example, the input string "HELLo"
should be converted to "hello"
, where all uppercase letters are changed to their lowercase equivalents while other characters remain the same.
In this article, youāll learn how to convert uppercase strings to lowercase in Java using different approaches. Weāll cover example programs, detailed logic, and step-by-step explanations to help you understand the process clearly.
š Required Knowledge
- Java programming basics
- Java operators
- Loop concepts
- Conditional statements
- ASCII values for characters
š Problem Statement
We need to write a Java program that converts an uppercase string to a lowercase string. The program should accept an input string and convert all uppercase alphabetic characters (AāZ
) to their corresponding lowercase characters (aāz
), while leaving all existing lowercase letters, numbers, and special characters unchanged.
This conversion will be performed by either using built-in methods or manual logic.
Letās understand this with a simple example
Example:
For a given input String str āHELLO123, Output should be āhello123.
Explanation:
Input String str: "HELlO123"
The String str contains uppercase characters: 'H', 'E', 'L', and 'O' and 'l' is lowercase, so it remain unchanged.
Additionally, It includes digits: '1', '2' and '3', which also remain unchanged.
After conversion, all the uppercase letters will change into lowercase letters.
Therefore, the resulting string from the input "HELLO123" will be "hello123".
š” Logic to convert uppercase string to lowercase
There are multiple logics to convert uppercase strings to lowercase strings in the java programming language.
Logic 1: Using in-built toLowerCase() function
- In Java, the most straightforward way to convert an uppercase string to a lowercase string is by using the built-in
toLowerCase()
method provided by theString
class. - This method automatically scans each character of the string and converts any uppercase letters (
AāZ
) to their corresponding lowercase letters (aāz
). Any characters that are already lowercase, numeric, or special symbols (such as@
,#
,1
, etc.) remain unchanged.
Logic 2: Using for loop
- We can use for loop to convert uppercase string to lowercase string. We have to take an input String str from user.
- Convert the input string to a character array using toCharArray() .
- Iterate through each character in the character array using for loop.
- Check if each character is an uppercase letter using an if-else statement and comparing its ASCII value (65 to 90).
- If the character is uppercase, add 32 to its ASCII value to convert it to lowercase.
- Cast the resulting ASCII value back to a character and assign it back to the array.
- Repeat this process until all characters have been checked.
- After checking all characters in the String str exit the loop. After exiting the loop, the character array contains the modified text with uppercase letters converted to lowercase.
- Finally, convert the modified character array back into a string and print the lowerString to display the output.
Java program to convert uppercase string to lowercase
ā Program 1: Using built-in toLowerCase() function
In the below program we are using toLowerCase() method to convert uppercase string to lowercase string.
import java.util.Scanner;
public class UcToLc {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the string");
String str = s.nextLine();
String reslutLowerCase= str.toLowerCase();
System.out.println("After Lowercase Conversion: "+reslutLowerCase);
s.close();
}
}
Output:
Enter the string
INTERVIEW EXPERT IS A GREAT LEARNING PLATFORM
After Lowercase Conversion: interview expert is a great learning platform
š§ Explanation:
- The java program imports the
Scanner
class from thejava.util
package. - It defines a class named
UcToLc
. - The
main
method is the entry point of the program. - It creates a
Scanner
object nameds
to read input from the user. - It asks the user to enter a string.
- Read the input string using
s.nextLine()
and store it in the variablestr
. - Convert the string to lowercase using the
toLowerCase()
method and store the result inreslutLowerCase
. - Print the converted lowercase string to the console.
- It closes the
Scanner
object to release system resources.
ā Program 2: Using for loop
In the below program we are using for loop to convert uppercase string to lowercase string.
import java.util.Scanner;
public class ConvertUCToLC {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Please enter a string");
String str = s.nextLine();
char ch [] = str.toCharArray();
for(int i=0; i<ch.length;i++) {
if(ch[i]>='A' && ch[i]<='Z') {
ch[i] = (char) (ch[i]+32);// ASCII relation A=65,a =97
// 65+32=97
}
}
String lowerString = new String(ch);
System.out.println("After Lowercase Conversion: "+lowerString);
s.close();
}
}
Output:
Please enter a string
@LOVE StUDY!2
After Lowercase Conversion: @love study!2
š§ Explanation:
- The java program imports the
Scanner
class from thejava.util
package. - It defines a class named
ConvertUCToLC
. - The
main
method is the entry point of the program. - It creates a
Scanner
object nameds
to read input from the user. - It asks the user to enter a string.
- It reads the text entered by the user using
nextLine()
method ofScanner
class and stores it in aString
variable named str. - It converts the
String
str into a character array using thetoCharArray()
method and stores it in a char array namedch
. - It iterates through each character in the
ch
array using a traditional for loop. - For each character at index
i
, if it’s an uppercase letter, it adds 32 to its ASCII value, effectively converting it to its corresponding lowercase letter. - After converting the uppercase letters to lowercase in the
ch
array. - The modified character array is converted back to a string and store it in lowerString variable print the lowerString to get the output..
- It closes the
Scanner
object to release system resources.
ā Program 3 : Using StringBuilder
public class ConvertLowercaseWithBuilder {
public static void main(String[] args) {
String upper = "HeLLO JAVA!";
StringBuilder sb = new StringBuilder();
for (char c : upper.toCharArray()) {
if (c >= 'A' && c <= 'Z') {
sb.append((char)(c + 32));
} else {
sb.append(c);
}
}
System.out.println("Input String: " + upper);
System.out.println("After Lowercase Conversion: " + sb.toString());
}
}
Output
Input String: HeLLO JAVA!
After Lowercase Conversion: hello java!
š§ Explanation:
- Input Initialization:
- We start with a sample string
upper = "HeLLO JAVA!"
, which contains both uppercase and lowercase letters along with spaces and symbols.
- We start with a sample string
- StringBuilder Declaration:
- We use
StringBuilder sb = new StringBuilder();
instead of string concatenation to improve performance when building the final output string.
- We use
- Loop Through Characters:
- We use an enhanced
for
loop withupper.toCharArray()
to iterate over each character of the string.
- We use an enhanced
- Character Conversion Logic:
- Inside the loop, we check if the character
c
is an uppercase letter by verifying if it’s between'A'
and'Z'
. - If it is, we convert it to lowercase by adding
32
to its ASCII value (as uppercase letters are 32 places before lowercase letters in ASCII). - If itās not an uppercase letter, we simply append it as it is.
- Inside the loop, we check if the character
- Final Output:
- After processing all characters, we print the original and converted lowercase string using
sb.toString()
.
- After processing all characters, we print the original and converted lowercase string using
ā Program 4: Using Java 8 Streams
import java.util.stream.Collectors;
public class StreamLowercaseConversion {
public static void main(String[] args) {
String phrase = "HeLLO JAVA!";
String result = phrase.chars()
.mapToObj(c -> Character.isUpperCase(c) ? (char)(c + 32) : (char)c)
.map(String::valueOf)
.collect(Collectors.joining());
System.out.println("Input String: " + phrase);
System.out.println("String After Lowercase Conversion: " + result);
}
}
Output
Input String: HeLLO JAVA!
String After Lowercase Conversion: hello java!
š§ Explanation:
Input String:
- We start with
String phrase = "HeLLO JAVA!";
, which contains uppercase letters and special characters.
Convert String to Stream of Characters:
phrase.chars()
converts the string into anIntStream
of ASCII values for each character.
Use mapToObj()
to Process Each Character:
- We apply a lambda function inside
mapToObj()
to convert each character:- If itās an uppercase letter (
Character.isUpperCase(c)
), we convert it by adding32
to its ASCII value. - Otherwise, we keep the character unchanged.
- If itās an uppercase letter (
- The result is a stream of
Character
objects.
Convert Characters to Strings:
.map(String::valueOf)
turns eachCharacter
object into aString
.
Join the Characters:
.collect(Collectors.joining())
merges all the characters into one complete string.
Print the Output:
- Finally, we print the original string and the newly formed lowercase version.
Conclusion
The java program we did here converts uppercase strings to lowercase strings by using built-in toLowerCase() function, for loop, Java 8 Stream etc.