Convert String to Lowercase in Java Without Using String Library
Converting a string to lowercase without using built-in String methods helps beginners understand how characters are represented internally and how ASCII values work.
In this blog, you will learn:
- How uppercase and lowercase characters are stored
- ASCII value logic for case conversion
- Step-by-step examples
- A Java program without using String library methods
- Practice challenges
Understanding ASCII Values
In ASCII:
- Uppercase letters ('A' to 'Z') → 65 to 90
- Lowercase letters ('a' to 'z') → 97 to 122
The difference between uppercase and lowercase letters is 32.
How the Logic Works
To convert a string to lowercase manually:
- Traverse each character of the string.
- If the character is between 'A' and 'Z', add 32.
- Leave other characters unchanged.
Example
Input: "Java PROGRAMMING 101"
- 'J' → 'j'
- 'P' → 'p'
- '101' → unchanged
Output: "java programming 101"
Java Program to Convert String to Lowercase
import java.util.Scanner;
public class StringToLowercase {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = sc.nextLine();
String result = "";
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
if (ch >= 'A' && ch <= 'Z') {
ch = (char) (ch + 32);
}
result = result + ch;
}
System.out.println("Lowercase String: " + result);
}
}
Sample Output
Enter a string: HELLO World
Lowercase String: hello world
Practice Challenges
- Convert a string to uppercase without using String library methods.
- Toggle the case of each character in a string.
- Count uppercase and lowercase characters in a string.
Manual case conversion builds a strong foundation in character handling and ASCII logic. These concepts are commonly tested in interviews and competitive programming.