之前做的将整数反转输出,这个是将字符串按照空格分开后进行反转输出
public static String reverse(String s) {
String[] array = s.split(" ");
String result = "";
int l = array.length;
//将字符串数组反转
for (int i = 0; i < (l - 1) / 2; i++) {
String temp = array[i];
array[i] = array[l - i - 1];
array[l - i - 1] = temp;
}
for (int i = 0; i < l; i++) {
result += array[i] + " ";//将反转后的字符串拼接起来
}
return result;
}
public static void main(String[] args) {
String s = "this is a test by chuanzhangStudio";
System.out.println("the original string is:" + s);
System.out.println("the reversed string is:" + StringReverse.reverse(s));
}
}