Convert String to Uppercase in Java Without Using String Library
Converting a string to uppercase without using built-in String methods helps understand character encoding and low-level string manipulation.
In this blog, you will learn:
- How uppercase and lowercase characters work internally
- 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:
- Lowercase letters ('a' to 'z') → 97 to 122
- Uppercase letters ('A' to 'Z') → 65 to 90
The difference between lowercase and uppercase letters is 32.
How the Logic Works
To convert a string to uppercase manually:
- Traverse each character of the string.
- If the character is between 'a' and 'z', subtract 32.
- Leave other characters unchanged.
Example
Input: "Java Programming 101"
- 'J' → already uppercase
- 'a' → 'A'
- 'v' → 'V'
- 'a' → 'A'
- Digits and spaces remain unchanged
Output: "JAVA PROGRAMMING 101"
Java Program to Convert String to Uppercase
import java.util.Scanner;
public class StringToUppercase {
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("Uppercase String: " + result);
}
}
Sample Output
Enter a string: java is fun
Uppercase String: JAVA IS FUN
Practice Challenges
- Convert a string to lowercase without using String library methods.
- Toggle the case of each character in a string.
- Count uppercase and lowercase characters separately.
Manually converting strings using ASCII values deepens your understanding of how characters are stored and processed internally. This knowledge is useful in interviews and low-level programming.