java反转字符串,不改变标点符号位置 面试题
public class test3 {
//写一个方法要求把字符串的字符倒叙输出但是符号位置保持原位置
public static void main(String[] args) {
String ss = “abc_defg”;
String exchange = exchange(ss.toCharArray());
System.out.println(exchange);
}
//判断是否是字母
public static boolean isLetter(char letter){
if(letter >= ‘a’ && letter <= ‘z’ || letter >= ‘A’ && letter <= ‘Z’){
return true;
}
return false;
}
//如果不是字母则交换位置
public static String exchange(char[] strArr){
for (int i = 0,j = strArr.length-1;i<j;i++,j–){
if(isLetter(strArr[i]) && isLetter(strArr[j])){
char a = strArr[i];
strArr[i] = strArr[j];
strArr[j] = a;
}
}
return new String(strArr);
}
}