java实现腾讯笔试反转字符串中的单词

题目描述:将输入的字符串中的单词进行反转,标点符号不进行反转,例子:I am a good man. -> man. good a am I

import java.io.IOException;
import java.util.Scanner;

public class maintest {

public static void main(String[] args) throws IOException {

String res = “”;
int temp = 0;
Scanner scanner = new Scanner(System.in);
String string = scanner.nextLine();

if (string.length()>0) 
{
for (int i = 0; i < string.length(); i++) {
char t = string.charAt(i);
System.out.println(t);
if (t == ‘ ‘) {
for (int j = i; j >= temp ; j–) {
res = string.charAt(j) + res;
}
temp = i+1;
}
if (i==string.length()-1) {
res = ” “+res;
for (int j = i; j >= temp ; j–) {
res = string.charAt(j) + res;
}
}
}
}

  System.out.println(res);
}

}

点赞