从控制台接收用户输入的一行英文句子,把句子的最前面两个单词移到句子的最后面去,并整理句子的大小写及标点符号,将新的句子输出。
input: The dog ran through the gate and down the street.
output: Ran through the gate and down the street, the dog?
input: My coat was too heavy for this warm day!
output: Was too heavy for this warm day, my coat?
(81条消息) Java split方法详细讲解_一只光头猿的博客-CSDN博客_java split
(81条消息) Java字符串(超超超详细)_白巧克力LIN的博客-CSDN博客_java 字符串
(82条消息) java数组删除数组元素_如何在Java中删除数组元素_cunchi4221的博客-CSDN博客
package t1;
import java.util.Scanner;
public class T1 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String x=in.nextLine();
String[] X=x.split(" ");
String[] Y= {"0","0"};//用于提取前两个词
char[] M=X[X.length-1].toCharArray();
char[] M_new=new char[M.length-1];
for(int i=0;i<M_new.length;i++) {
M_new[i]=M[i];
}
X[X.length-1]=new String(M_new);//把最后的标点符号去掉了
for(int i=0;i<2;i++) {
Y[i]=X[i];
}
M=Y[0].toCharArray();
M[0]=(char)(M[0]+'a'-'A');
Y[0]=new String(M);//把前两个单词抠出来,并把第一个词的第一个字母转成小写
M=Y[1].toCharArray();
M_new=new char[M.length+1];
for(int i=0;i<M.length;i++) {
M_new[i]=M[i];
}
M_new[M.length]='?';
Y[1]=new String(M_new);//在第二个词后加问号
for(int i=0;i<X.length-2;i++) {
X[i]=X[i+2];
}
M=X[0].toCharArray();
M[0]=(char)(M[0]+'A'-'a');
X[0]=new String(M);//将原字符串数组前两位删除
M=X[X.length-3].toCharArray();
M_new=new char[M.length+1];
for(int i=0;i<M.length;i++) {
M_new[i]=M[i];
}
M_new[M.length]=',';
X[X.length-3]=new String(M_new);//加逗号
for(int i=0;i<X.length-2;i++) {
System.out.print(X[i]+" ");
}
for(int i=0;i<Y.length;i++) {
System.out.print(Y[i]+" ");
}
}
}