
I want to convert a string of mix cases(lower and upper case) to vice versa.
ex.
HellO should be printed to hELLo.
the string comes from user using datainputstream. Also sending individual chars of string to toUpperCase() and toLowerCase() not possible.

Java String Case Converter
import java.util.*;
class StringExample {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter string: ");
String str = input.nextLine();
StringBuffer sb = new StringBuffer();
char ch[] = str.toCharArray();
for (int i = 0; i < ch.length; i++) {
if (ch[i] == ' ') {
sb.append(" ");
}
if (Character.isLowerCase(ch[i])) {
char cha = Character.toUpperCase(ch[i]);
sb.append(Character.toString(cha));
} else if (Character.isUpperCase(ch[i])) {
char cha = Character.toLowerCase(ch[i]);
sb.append(Character.toString(cha));
}
}
System.out.println(sb.toString() + " ");
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.