例如:"Ni Hao Katherine"变形之后就变成了"nI hAO kATHERINE"
public class Test {
public static void main(String[] args) {
String s = "Ni Hao Katherine";
String str = "";
for(int i = 0,len = s.length();i<len;i++) {
//获取当前字符
char c = s.charAt(i);
if(c !=' ') {
c = (char)(c >= 'A' && c <= 'Z'?(c+32):(c-32));
}
str = str + c;
}
System.out.println(str);
//第二步:单词反转
String word = "";
String ret = "";
for(int i = 0,len = str.length();i<len;i++) {
char c = str.charAt(i);
if(c == ' ') {
ret = word+" "+ret;
word = "";
}else {
word = word+c;
}
}
ret = word+" "+ret;
System.out.println(ret);
}
}
首先获取当前字符,再单词反转,输出单词
运行结果
nI hAO kATHERINE
kATHERINE hAO nI