Convert String to Uppercase in Java Without Using String Library

Learn how to convert a string to uppercase in Java without using built-in String methods. This blog explains the ASCII-based logic with examples, a Java program, and practice challenges.

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:

Understanding ASCII Values

In ASCII:

The difference between lowercase and uppercase letters is 32.

How the Logic Works

To convert a string to uppercase manually:

  1. Traverse each character of the string.
  2. If the character is between 'a' and 'z', subtract 32.
  3. Leave other characters unchanged.

Example

Input: "Java Programming 101"

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

  1. Convert a string to lowercase without using String library methods.
  2. Toggle the case of each character in a string.
  3. 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.