测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用1个空格分开,输入保证句子末尾没有多余的空格。
每个测试用例的输出占一行,输出倒序后的句子。
Hello World Here I Come
Come I Here World Hello
#include <stdio.h>
#include <string.h>
int main(){
char a[81];
gets(a);
int i, j, l = strlen(a);
for(i = l - 1; i >= 0; i--){
if(a[i - 1] == ' ' || i == 0){
for(j = i; j < l; j++){
if(a[j] == ' ' || a[j] == '\0') break;
else printf("%c", a[j]);
}
if(i > 0) printf(" ");
}
}
printf("\n");
return 0;
}
#include <iostream>
#include <string>
#include <stack>
#include <cstdio>
#include<sstream>
using namespace std;
int main(){
string word;
char ch[81];
gets(ch);
stringstream ss(ch);
stack<string> st;
while(ss >> word)
st.push(word);
cout<<st.top();
st.pop();
while(st.size()){
cout<<" "<<st.top();
st.pop();
}
return 0;
}
注意:判断到最后一个单词时,后面不加空格。
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
String[] s=str.split(" ");
StringBuilder sb=new StringBuilder();
for(int i=s.length-1;i>=0;i--){
sb.append(s[i]);
if(i==0){
break;
}
sb.append(" ");
}
System.out.println(sb.toString());
}
}
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String line=sc.nextLine();
String[] list=line.split(" ");
for(int i=list.length-1;i>0;i--){
System.out.print(list[i]+" ");
}
System.out.print(list[0]);
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
String[] array = str.split(" ");
for(int i=0;i<array.length;i++){
if(i==array.length-1){
System.out.print(array[array.length-1-i]);
}else{
System.out.print(array[array.length-1-i]+" ");
}
}
//br.close;
}
}
#include <iostream>
#include <string>
using namespace std;
int main(){
string str;
string zhan[10000];
int n,s=0,e=0;
while(getline(cin,str)){
string one_str="";
for(int i=0;i<str.length();i++){
if(str[i]!=' '){
one_str=one_str+str[i];
}
else{
zhan[n]=one_str;
one_str="";
n=n+1;
}
}
zhan[n]=one_str;
for(int j=n;j>=0;j--){
cout<<zhan[j];
if(j!=0) cout<<" ";
}
cout<<endl;
}
} import java.util.Scanner;
public class StringSplit
{
@SuppressWarnings("resource")
public String init()
{
System.out.println("请输入一个字符串 : ");
String str ;
Scanner _in = new Scanner(System.in);
str = _in.nextLine();
return str;
}
public void split(String str)
{
//split函数依据字符分开字符串,并存入String数组
String[] strArray = str.split(" ");
for(int i=strArray.length-1;i>=0;i--)
{
System.out.print(strArray[i]+" ");
}
}
public static void main(String[] args)
{
StringSplit s = new StringSplit();
s.split(s.init());
}
}
public class shuofanhua {
public static void main(String[] args) {
Scanner sc = new Scanner(System. in );
String str = "" ;
str = sc.nextLine();
int temp = 0,j = 0;
char [] ch = str.toCharArray();
for ( int i = 0; i < ch. length ; i++) {
if (ch[i] == ' ' ) {
temp+=1;
}
}
String[] fanhua = new String[temp+1];
for ( int i = 0; i < temp+1; i++)
{
fanhua[i] = "" ;
}
for ( int i = 0; i < ch. length ; i++)
{
if (ch[i] == ' ' )
{
j+=1;
}
else {fanhua[j] += ch[i];}
}
for ( int i = j; i > 0;i-- )
{
System. out .print(fanhua[i]+ " " );
}
System. out .print(fanhua[0]);
}
}
#include <iostream>
using namespace std;
int main() {
string str;
getline(cin, str);
int len = str.length();
int j = len - 1;
while (j >= 0) {
for (; str[j] != ' ' && j > 0; --j) {
}
if (j == 0)
for (int i = j; str[i] != ' ' && str[i] != '\0'; ++i) {
cout << str[i];
}
else {
for (int i = j + 1; str[i] != ' ' && str[i] != '\0'; ++i) {
cout << str[i];
}
printf(" ");
}
--j;
}
return 0;
} #include <iostream>
#include <stack>
using namespace std;
int main() {
string temp;
stack<string> mystack;
while(cin>>temp){
mystack.push(temp);
if(getchar()=='\n') break;
}
while(!mystack.empty()){
cout<<mystack.top();
mystack.pop();
if(mystack.empty()) break;
cout<<" ";
}
cout<<endl;
return 0;
} /*说反话*/
#include<cstdio>
(802)#include<iostream>
#include<cstring>
using namespace std;
int main()
{
string str;
getline(cin, str);
while(str.length() > 0 )
{
//str.rfind()是从字符串右边开始向左找
int i = str.rfind(" "); //从右向左找到第一个空格,并且返回右边第一个空格的pos
if(i != string::npos) //如果找到了
{
cout<<str.substr(i+1)<<" "; //子串的substr(pos)的长度为 len = length()-pos+1;
string tmp = str.substr(0,i); //返回右边第一个空格前的子串
str = tmp;
}
else
{
//如果没有找到 ,说明没有空格,只有一个不带空格的字符串, 直接打印字符串
cout<<str<<endl;
break;
}
}
return 0;
}