How to convert lowercase to uppercase in java using ASCII ?

Here, we are supposed to create a source code that asks for String input from the user and displays the string by changing the String’s Case to both UpperCase and LowerCase.

The basic idea is to change the input string’s case using ASCII Values.

The ASCII Values are:
A-Z = 65 to 90
a-z = 97 to 122
0-9 = 48 to 57

Algorithm(Convert lowercase to uppercase or uppercase to lowercase using ASCII)

  1. Prompt the user to enter a string and store it in a variable.
  2. Use a for loop to iterate through each character in the string.
  3. Check if the character is a lowercase letter by comparing its ASCII value to the range 97-122. If it is, subtract 32 from its ASCII value and convert it back to a character. If it is an uppercase letter, check if its ASCII value is within the range of 65-90. If it is, add 32 to its ASCII value and convert it back to a character. Otherwise, leave the character unchanged.
  4. Add the modified character to a separate string depending on whether it was originally uppercase or lowercase.
  5. Display the final results by printing both strings.

Code

/* Program to enter a string and display in both UpperCase and LowerCase using ASCII Values.*/

import java.util.*;

import java.util.*;

class ChangeCase {
  public static void main() {
    Scanner scanner = new Scanner(System.in);
    System.out.print("\nEnter a string: ");
    String inputString = scanner.nextLine();
    int i, asciiValue;
    char originalChar, modifiedChar;
    String uppercaseString = "";
    String lowercaseString = "";

    // Loop to convert lowercase to uppercase
    for (i = 0; i < inputString.length(); i++) {
      originalChar = inputString.charAt(i);
      asciiValue = (int) originalChar;
      if (asciiValue >= 97 && asciiValue <= 122) {
        asciiValue -= 32;
        modifiedChar = (char) asciiValue;
      } else {
        modifiedChar = originalChar;
      }
      uppercaseString += modifiedChar;
    }
    System.out.println("\nUppercase String: " + uppercaseString);

    // Loop to convert uppercase to lowercase
    for (i = 0; i < inputString.length(); i++) {
      originalChar = inputString.charAt(i);
      asciiValue = (int) originalChar;
      if (asciiValue >= 65 && asciiValue <= 90) {
        asciiValue += 32;
        modifiedChar = (char) asciiValue;
      } else {
        modifiedChar = originalChar;
      }
      lowercaseString += modifiedChar;
    }
    System.out.println("\nLowercase String: " + lowercaseString);
  }
}

Output:

Enter String: World Wide Web

Upper Case: WORLD WIDE WEB
Lower Case: world wide web


Enter String: Great Wall of China

Upper Case: GREAT WALL OF CHINA
Lower Case: great wall of china
How to convert lowercase to uppercase in java using ASCII ?