How to convert lowercase to uppercase in Java without using any library function:
In this post, we will learn how to convert a lowercase string to uppercase in Java without using any library function.
For example, if the given string is hello world, it will convert it to HELLO WORLD.
The program will take the string as input from the user.
Algorithm:
The difference between the ASCII values of lowercase and uppercase values of a character is 32. We can subtract 32 from the ASCII value of a lowercase character to get the ASCII value of its uppercase.
We will follow the below algorithm:
- Take the string as input from the user.
- Iterate through the characters of the string one by one.
- Create an empty StringBuilder object.
- Check if the character is uppercase or lowercase.
- If it is a lowercase, subtract 32 from it and convert the value to character by using type conversion. Append it to the StringBuilder.
- Else, append the character to the StringBuilder without making any change to it.
- Once the iteration is done, convert the StringBuilder to a string.
Java program:
Below is the complete program:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str;
StringBuilder stringBuilder = new StringBuilder();
System.out.println("Enter a string: ");
str = sc.nextLine();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {
stringBuilder.append((char) (str.charAt(i) - 32));
} else {
stringBuilder.append(str.charAt(i));
}
}
System.out.println(stringBuilder);
}
}
Here,
- sc is a Scanner object to read the user input string.
- str is a String variable to hold the user input string.
- stringBuilder is a StringBuilder object to create the final string. We will keep appending the characters to the string builder object to build the result string.
- The for loop is iterating through the characters of the string one by one. It checks if the current iterating character is lowercase or not in the if block.
- It uses charAt to get the character at that position and compares it with ‘a’ and ‘z to find out if it is a lowercase or uppercase character.
- It converts the character to uppercase by subtracting 32 from it and converts it back to a character and appends it to the StringBuilder.
- If it is not a lowercase character, it appends that character directly to the StringBuilder.
- Once the for loop ends, it prints the value of the string builder.
If you run this program, it will print output as like below:
Enter a string:
Hello 123 World !!
HELLO 123 WORLD !!
Enter a string:
hello world
HELLO WORLD
Method 2: By using a different method:
We can also use a different method to do the lowercase to uppercase conversion. For example:
import java.util.Scanner;
class Main {
private static String convertToUppercase(String str) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {
stringBuilder.append((char) (str.charAt(i) - 32));
} else {
stringBuilder.append(str.charAt(i));
}
}
return stringBuilder.toString();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str;
System.out.println("Enter a string: ");
str = sc.nextLine();
System.out.println(convertToUppercase(str));
}
}
Here, I have created a new method convertToUppercase to convert the lowercase string to uppercase. It takes one String as the parameter and returns one string back.
We are converting the StringBuilder to a string by using the toString() method.
You might also like:
- Java program to add characters to the middle of a string
- How to remove the first element of an ArrayList in Java
- Java program to check if a number is Automorphic or not
- Java program to check if two numbers are Amicable or not
- Java program to check if a number is a happy number or not
- Java program to print the Neon numbers from 0 to 10000
- How to remove the first character of a string in Java
- How to remove the last character of a string in Java