Convert to Upper Case
System.out.print("Enter A String ~>");
str = scan1.nextLine(); // assume enter lower case
ChangeCh(str);
private static void ChangeCh(String str) {
// TODO Auto-generated method stub
System.println(str.toUpperCase());
}
For converting to lower case, its quite similar with the above answer, you just need to modify a bit. I think you can solve it yourself.
Source: Java - String toLowerCase() Method.
To convert Upper Case
to Lower Case
and Lower Case
to Upper Case
between a String
, use the code below
public static void main(String args [])throws IOException
{
System.out.println("Enter A String ~>");
Scanner scan1 = new Scanner(System.in);
String str = scan1.nextLine();
String sentence = "";
for(int i=0;i<str.length();i++)
{
if(Character.isUpperCase(str.charAt(i))==true)
{
char ch2= (char)(str.charAt(i)+32);
sentence = sentence + ch2;
}
else if(Character.isLowerCase(str.charAt(i))==true)
{
char ch2= (char)(str.charAt(i)-32);
sentence = sentence + ch2;
}
else
sentence= sentence + str.charAt(i);
}
System.out.println(sentence);
}
Note : A (upper case) and a (lower case) have a difference of 32 in ASCII code.