Java Program to convert lower case to upper case
Objective
Write a Java program to convert a lowercase string to uppercase.
Algorithm / Approach
- Accept a string from the user and store it in
s1. - Call the built-in
toUpperCase()method ons1. - Store the result in
s2. - Print
s2.
Test.java
import java.util.Scanner;
class Test {
public static void main(String[] a)
{
Scanner s=new Scanner(System.in);
System.out.print("Enter String: ");
String s1 = s.nextLine();
String s2 = s1.toUpperCase();
System.out.print("Upper Case = "+s2);
}
}
Expected Output
Enter String: java Prowess Upper Case = JAVA PROWESS
Explanation of the Program
- Java provides highly optimized built-in methods for case conversion:
toUpperCase()andtoLowerCase(). - These methods automatically handle characters correctly and ignore numbers or special punctuation marks without throwing errors.
- Because strings are immutable, the original
s1string remains untouched, and a new uppercase string is returned.
Complexity
Time Complexity
O(n)
Space Complexity
O(n)