题目:将大小字母反转
思路:大小字母对应的编码的值为32
package huaweiicomeagain;
/**
* 大小字母反正
* @author huanghuankun
*
*/
public class CharReverse {
public static void main(String[] args){
String srcStr = "SFDFWEFdefewfwDFEWF";
String regex = "^[A-Z a-z]*$";
boolean flag = srcStr.matches(regex);
System.out.println("srcStr="+srcStr);
if(flag){
String dstStr = charReverse(srcStr);
System.out.println("dstSrc="+dstStr);
}else{
System.out.println("Wrong input.");
}
}
private static String charReverse(String srcStr) {
// TODO Auto-generated method stub
String dstStr = "";
char ch;
for(int i=0;i<srcStr.length();i++){
if('A'<srcStr.charAt(i) && srcStr.charAt(i)<'Z'){
ch = (char) (srcStr.charAt(i)+32);
dstStr += String.valueOf(ch);
}else{
ch = (char) (srcStr.charAt(i)-32);
dstStr += String.valueOf(ch);
}
}
return dstStr;
}
}
结果:
srcStr=SFDFWEFdefewfwDFEWF
dstSrc=sfdfwefDEFEWFWdfewf