Auto commit

上级 5b0ce43e
import java.util.*;
class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
char[] ch = str.toCharArray();
int len = ch.length;
//逆置整个字符串
reverse(ch,0,len-1);
//逆置每个单独的单词
int i = 0;
while (i < len){
int j = i;
while (j < len && ch[j] != ' '){
j++;
}
if (j < len){
reverse(ch,i ,j-1);
i = j + 1;
}else {
//此时逆置单词已经到了最后一个
reverse(ch, i, j-1);
i = j;
}
}
// 将字符数组准转换为字符串
String s = new String(ch);
System.out.println(s);
}
public static void reverse(char[] arr, int start, int end){
while (start <end){
char tmp = arr[start];
arr[start] = arr[end];
arr[end] = tmp;
start++;
end--;
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册