In Java, we can convert a lowercase string to an uppercase string using either the built-in toUpperCase()
method or custom logic using loops.
For example, if the input string is "Java"
, it should be converted to "JAVA"
.
In this article, we will explore how to convert a lowercase string to uppercase using different approaches, along with a detailed explanation of the logic and program to enhance your understanding.
š Required Knowledge
- Java programming basics
- Java operators
- Loop concepts
- Conditional statements
- ASCII values for characters
š Problem Statement
We need to write a Java program that converts a lowercase string to uppercase. The program should take an input string and convert all lowercase letters to their corresponding uppercase form, while leaving any existing uppercase letters, numbers, and special characters unchanged.
This task will be performed in our program. Let’s see an example,
Example:
For a given input String str “hI$11”, Output should be “HI$11”.
Explanation:
Input String str: "hI$11"
String str contains lowercase characters: 'h'. The lowercase letter 'l' remains unchanged.
And, str includes digits: '1', '1', which are also remain unchanged.
str also contains a special character: $, it is also remains same.
After conversion, 'h' is converted to Uppercase 'H'.
Therefore, the resulting string from the input "hI$11" will be "HI$11".
š” Logic to convert lowercase string to uppercase
There are multiple logics to convert lowercase strings to uppercase strings in the java programming language.
Logic 1: Using built-in toUpperCase() function
- In Java, we utilize the toUpperCase() method to convert all lowercase strings to uppercase strings.
- In Java, the simplest and most efficient way to convert a lowercase string to uppercase is by using the built-in
toUpperCase()
method provided by theString
class. - This method scans each character of the string and automatically converts any lowercase letters (
a-z
) to their corresponding uppercase letters (A-Z
). All non-lowercase charactersāsuch as digits, special symbols, or already-uppercase lettersāare left unchanged.
Logic 2: Using for loop
- To convert lowercase string to uppercase string using for loop, we need to take an input String str.
- Convert the input string to a character array using toCharArray(). This stores each character of the string in an array.
- Using loop, iterate through each character in the character array.
- For each character at current index, using if-else check if it is a lowercase letter by comparing its ASCII value. Lowercase letters in ASCII have values from 97 (‘a’) to 122 (‘z’).
- If the character is a lowercase letter (i.e., its ASCII value is between 97 and 122 inclusive), convert it to the corresponding uppercase letter by subtracting 32 from its ASCII value.
- Use the (char) cast to convert the resulting ASCII value back to a character and assign it back to the array.
- It will continue iterating through each character in the array until all characters have been processed, then it will exit the loop
- After exiting the loop, the character array contains the input text with all lowercase letters converted to uppercase letters. And convert the modified character array into string.
Java program to convert lowercase string to uppercase
ā Program 1: Using built-in toUpperCase() function
In the below program we are using toUpperCase() method to convert lowercase string to uppercase string.
import java.util.Scanner;
public class LcToUc {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the string");
String str = s.nextLine();
String reslutUpperCase= str.toUpperCase();
System.out.println("Lowercase string is converted to Uppercase: "+reslutUpperCase);
s.close();
}
}
Output:
Enter the string
interview expert
Lowercase string is converted to Uppercase: INTERVIEW EXPERT
š§ Explanation:
- The java program imports the
Scanner
class from thejava.util
package. - The program defines a class named
LcToUc
. - The
main
method is the entry point of the program. - It creates a
Scanner
object nameds
to read input from the user. - It asks the user to enter a string.
- It reads the text entered by the user using
nextLine()
method ofScanner
class and stores it in aString
variable named str. - It converts the entire str string to uppercase using the
toUpperCase()
method and stores the result in a newString
variable namedresultUpperCase
. - It prints the converted uppercase string using
System.out.println()
. - It closes the
Scanner
object to release system resources.
ā Program 2: Using for loop
In the below program we are using for loop to convert lowercase string to uppercase string.
import java.util.Scanner;
public class ConvertLCToUC {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Please enter a string");
String str = s.nextLine();
char ch [] = str.toCharArray();
for(int i=0; i<ch.length;i++) {
if(ch[i]>='a' && ch[i]<='z') {
ch[i] = (char) (ch[i]-32);// ASCII relation A=65,a =97
// 65=97-32
}
}
String upperString = new String(ch);
System.out.println(upperString);
s.close();
}
}
Output:
Please enter a string
Namastey
NAMASTEY
š§ Explanation:
- The java program imports the
Scanner
class from thejava.util
package. - The program defines a class named
ConvertLCToUC
. - The
main
method is the entry point of the program. - It creates a
Scanner
object nameds
to read input from the user. - It asks the user to enter a string.
- It reads the text entered by the user using
nextLine()
method ofScanner
class and stores it in aString
variable named str. - It converts the
String
str into a character array using thetoCharArray()
method and stores it in a char array namedch
. - It iterates through each character in the
ch
array using a traditional for loop. - For each character at index
i
, if it’s a lowercase letter, it subtracts 32 from its ASCII value, effectively converting it to its corresponding uppercase letter. - After converting the lowercase letters to uppercase in the
ch
array. - The modified character array is converted back to a string and stored in upperString and print it to display the result.
- It closes the
Scanner
object to release system resources.
Conclusion
The java program we did here converts lowercase string to uppercase string using built-in toUpperCase() method and for loop.