In this tutorial, we are going to java a java program to convert lowercase characters (alphabet) into uppercase characters (alphabet).
For any input string, any lowercase character used in input string will be replaced with uppercase character of same alphabet as lowercase character.
Converting characters from lowercase to uppercase is a common task in programming, especially when dealing with user inputs or data that needs to be standardized. In Java, this task can be accomplished in a few different ways, each suitable for different situations. This capability is particularly useful in applications where text needs to be uniform, such as entering names or cities into a database. In this guide, we’ll explore several methods to convert a lowercase character to an uppercase character in Java, showing how versatile and user-friendly Java can be for handling strings.
For example
Case1: if user inputs ‘Quescol’
The output should be ‘QUESCOL’
Case2: if user inputs ‘jaVa’
The output should be ‘JAVA’
Java Program to Convert Lowercase Character to Uppercase Character
Program 1: Using Character.toUpperCase() method
import java.util.*;
public class Main
{
public static void main (String[] args)
{
System.out.println("Java program to convert lowercase to uppercase");
Scanner sc = new Scanner (System.in);
System.out.println ("Please enter a String");
String str = sc.nextLine();
StringBuffer strBuffer = new StringBuffer(str);
for(int i = 0; i < str.length(); i++){
if(Character.isLowerCase(str.charAt(i))) {
strBuffer.setCharAt(i, Character.toUpperCase(str.charAt(i)));
}
}
System.out.println("After Conversion lowecase to uppercase");
System.out.println(strBuffer);
}
}
Output
Java program to convert lowercase to uppercase
Please enter a String
Quescol
After Conversion lowecase to uppercase
QUESCOL
Explanation
For the input string ‘Quescol’, all the lowercase letters i.e. ‘uescol’ is converted into respective uppercase i.e. ‘UESCOL’ While iterating through the string, the letters ‘u’, ‘e’, ‘s’, ‘c’, ‘o’, and ‘l’ return ‘True” as an output when is checked with ‘isLowerCase()’ function, then they are converted into respective uppercase letters using ‘isUpperCase()’ function and replaced simultaneously.
This Java program changes lowercase letters in a word to uppercase letters. When you run the program, it first tells you it’s going to convert lowercase to uppercase. Then, it asks you to type in a word. Once you do that, the program looks at each letter in the word. If the letter is in lowercase, the program changes it to uppercase. It does this by going through each letter one by one, and if the letter is small, it makes it big. After changing all the lowercase letters to uppercase, the program shows you the new word with all the big letters. This way, you can see how the lowercase letters in your original word have been changed to uppercase.
Program 2: Using String.toUpperCase() method
If you’re working with strings or need to convert a character that’s part of a string, you can use the String.toUpperCase()
method. This method converts all the characters in the string to uppercase, so you can extract the character you need afterward if necessary.
import java.util.*;
public class Main
{
public static void main (String[] args)
{
System.out.println("Java program to convert lowercase to uppercase");
Scanner sc = new Scanner (System.in);
System.out.println ("Please enter a String");
String str = sc.nextLine();
String upperCaseStr = str.toUpperCase();
System.out.println("After Conversion lowecase to uppercase");
System.out.println(upperCaseStr);
}
}
Output
Java program to convert lowercase to uppercase
Please enter a String
quescol
After Conversion lowecase to uppercase
QUESCOL
Explanation
This Java program helps you change all the lowercase letters in a word to uppercase letters. When you run the program, it first tells you that it will convert lowercase letters to uppercase. Then, it asks you to type in a word. Once you enter your word, the program quickly changes all the small letters in the word to big letters using a special command called toUpperCase(). After it has changed the letters, the program shows you the new word with all uppercase letters. This is a simple and fast way to see your word with all big letters instead of small ones.
Program 3: Using ASCII Value Manipulation
For those interested in how characters are represented in ASCII, you can convert a lowercase character to uppercase by manipulating its ASCII value. Lowercase and uppercase letters differ by a constant value (32), which you can subtract to make a character uppercase.
import java.util.*;
public class Main
{
public static void main (String[] args)
{
System.out.println("Java program to convert lowercase to uppercase");
Scanner sc = new Scanner (System.in);
System.out.println ("Please enter a Character");
Character str = sc.nextLine().charAt(0);
char upperCaseChar = (char) (str - 32);
System.out.println("After Conversion lowecase to uppercase");
System.out.println(upperCaseChar);
}
}
Output
Java program to convert lowercase to uppercase
Please enter a Character
q
After Conversion lowercase to uppercase
Q
Explanation
This Java program changes a single lowercase letter into an uppercase letter. When you start the program, it tells you that it will convert a lowercase letter to uppercase. It then asks you to type in one letter. Once you enter the letter, the program does a little math to change it from lowercase to uppercase by subtracting 32 from its ASCII code value. This is a neat trick because in the code system that computers use, uppercase and lowercase letters are exactly 32 units apart. After it changes the letter, the program shows you the uppercase version of the letter you typed in.