题目
将一个英文语句以单词为单位逆序排放。例如“I am a boy”,逆序排放后为“boy a am I”
所有单词之间用一个空格隔开,语句中除了英文字母外,不再包含其他字符
接口说明
/**
* 反转句子
*
* @param sentence 原句子
* @return 反转后的句子
*/
public String reverse(String sentence);
知识点 数组
运行时间限制 10M
内存限制 128
输入
将一个英文语句以单词为单位逆序排放。
输出
得到逆序的句子
样例输入 I am a boy
样例输出 boy a am I
==========================================
一次通过 100分
单词倒排【中级】
一次通过 200分
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
String str=scanner.nextLine();
String word=new String();
for(int i=str.length()-1;i>=0;i--)
{
char c=str.charAt(i);
if(c!=' ') {word+=c;}
else
{
for(int j=word.length()-1;j>=0;j--)
{System.out.print(word.charAt(j));}
System.out.print(" ");
word=new String();
}
}
for(int j=word.length()-1;j>=0;j--)
{System.out.print(word.charAt(j));}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
String str=scanner.nextLine();
String word=new String();
for(int i=str.length()-1;i>=0;i--)
{
char c=str.charAt(i);
if(('a'<=c&&c<='z')||('A'<=c&&c<='Z')) {word+=c;}
else
{
for(int j=word.length()-1;j>=0;j--)
{System.out.print(word.charAt(j));}
System.out.print(" ");
word=new String();
}
}
for(int j=word.length()-1;j>=0;j--)
{System.out.print(word.charAt(j));}
}
}