0
点赞
收藏
分享

微信扫一扫

字符串按单词翻转

何以至千里 2022-01-12 阅读 53
算法

例如:
Are you ready ?
翻转后:
? ready you Are

public class 字符串按单词翻转 {
 public static void main(String[] args) {
String  s = "Are you ready ?";
  System.out.println(reverse(s));
 }
 static String reverse(String S) {
  String s1 = reverseString(S);
  String [] words = s1.split(" ");
  StringBuffer sb = new StringBuffer();
  for (int i = 0; i <words.length ; i++) {
     sb.append(reverseString(words[i])+" ");
  }
  return  sb.deleteCharAt(sb.length()-1).toString();
 }
 static  String reverseString(String S) {
  StringBuffer stringBuffer = new StringBuffer(S);
  return stringBuffer.reverse().toString();

 }
}

举报

相关推荐

0 条评论