public class StringReverse { public static void main(String[] args) { String word = "china will be great again!"; String newWord = reverse(word); System.out.println(newWord); } public static String reverse(String word){ String[] words = word.split(" "); //判断是否只含有字母 String newWord=" "; for (String wd :words){ System.out.println(wd); if(isNumeric(wd)){ newWord+=" "+reverseWord(wd); }else { newWord+=" "+wd; } } return newWord; } public static boolean isNumeric(String word){ char[] wordChar = word.toCharArray(); boolean flag =false; for(int i=0;i<wordChar.length;i++){ if(((int)wordChar[i] >=65 && (int)wordChar[i]<=90) || ((int)wordChar[i] >=97 && (int)wordChar[i] <=122)){ flag = true; }else{ flag = false; } } return flag; } // 编写逆序逻辑 public static String reverseWord(String word){ char[] wordChar = word.toCharArray(); int length = wordChar.length-1; for (int i=0 ; i<wordChar.length && i<length;i++){ char temp = wordChar[i]; wordChar[i] = wordChar[length]; wordChar[length] = temp; length --; } String newword = new String(wordChar); return newword; } }
06-22
459
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-21
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交