×

Java Programs

Java Practice

Java program to convert string to lowercase and uppercase

Here, we will learn how to convert string into Lowercase and Uppercase using String.toLowerCase() and String.toUpperCase() methods in java?

String.toLowerCase()

It is predefined (built-in) method of String class in Java, it returns lowercase string.

String.toUpperCase()

It is predefined (built-in) method of String class in Java, it returns uppercase string.

Consider the program:

This an example of Java string programs, In this program a string is given in any case and we have to convert it into uppercase and lowercase.

/*Java program to convert string into Lowercase and Uppercase*/
import java.util.*; 
class ConvertCasePrg
{
   public static void main(String args[])
   {
		Scanner sc= new Scanner(System.in);
		String str="";
		
		//input string
		System.out.print("Enter any string: ");
		str=sc.nextLine();

		//declaring objects to store lowercase and uppercase strings
		String lowerCaseString="",upperCaseString="";

		//convert into lower case
		lowerCaseString= str.toLowerCase();  
		//convert into upper case
		upperCaseString= str.toUpperCase();  

		//printing the strings
		System.out.println("Original String: "+str);
		System.out.println("Lower Case String: "+lowerCaseString);
		System.out.println("Upper Case String: "+upperCaseString);   
   }
}

Output

Complie: javac ConvertCasePrg.java
Run: java ConvertCasePrg

Output

Enter any string: Hello Guys.
Original String: Hello Guys.
Lower Case String: hello guys.
Upper Case String: HELLO GUYS. 

Java String Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.