You have a problem here :
s1.charAt(i) = s1.charAt(i) - 32;
------------ -----------------
1 2
Here there are two problems :
- First thing, the 2nd part return an int, and you try to assign it to a char
- Second you can't make an assign like that
Instead I would use :
String s1 = "AbCd";
//create a char array
char[] array = s1.toCharArray();
//loop over this array, and work just with it
for (int i = 0; i < array.length; i++) {
if (array[i] >= 'a' && array[i] <= 'z') {
array[i] = (char) (s1.charAt(i) - 32);//<--------------------------note this
} else if (s1.charAt(i) >= 'A' && s1.charAt(i) <= 'Z') {
array[i] = (char) (s1.charAt(i) + 32);
}
}
//when you end, convert that array to the String
s1 = String.valueOf(array);//output of AbCd is aBcD
Beside I would like to use :
String result = "";
for (int i = 0; i < array.length; i++) {
if (Character.isLowerCase(array[i])) {
result += Character.toUpperCase(s1.charAt(i));
} else {
result += Character.toLowerCase(s1.charAt(i));
}
}