首页 > 试题广场 >

说反话 (20)

[编程题]说反话 (20)
  • 热度指数:6547 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。

输入描述:
测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用1个空格分开,输入保证句子末尾没有多余的空格。


输出描述:
每个测试用例的输出占一行,输出倒序后的句子。
示例1

输入

Hello World Here I Come

输出

Come I Here World Hello
啥头像
string = raw_input().split()
string.reverse()
print(' '.join(string))


发表于 2016-01-17 19:44:08 回复(0)
#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;
}

编辑于 2018-04-24 20:16:44 回复(1)
#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;
}


编辑于 2015-12-23 22:53:20 回复(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());
    }
}
发表于 2020-03-08 21:35:20 回复(0)
console.log(readline().split(' ').reverse().join(' '))

编辑于 2019-08-01 14:16:52 回复(0)
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]);
    }
}

发表于 2018-10-02 10:17:01 回复(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;
    }
}

发表于 2017-09-22 10:56:36 回复(0)
#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;
	}
}

发表于 2017-08-15 22:35:19 回复(0)
say = raw_input().split()
say = say[:: -1]
for i in range(len(say)):
    print say[i],

编辑于 2016-12-21 22:42:33 回复(0)
#include<cstdio>
int main(){
	int num = 0;
	char ans[90][90];
	while(scanf("%s", ans[num]) != EOF){
		num++;
	}
	for(int i = num - 1; i >= 0; i--){
		printf("%s", ans[i]);
		if(i > 0) printf(" ");
	}
	printf("\n");
	return 0;
} 

发表于 2016-03-16 10:33:50 回复(0)
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());
	}

}
java运行过了,这里过不了醉了。。。

发表于 2015-11-27 10:14:06 回复(1)

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]);

   }

}

发表于 2015-10-16 20:21:36 回复(0)
import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        String[] b= s.split("\\s");
        System.out.print(b[b.length-1]);
        for (int i =  b.length-2; i >= 0; i--) {
            System.out.print(" "+b[i]);
        }
    }
}

发表于 2015-06-04 16:42:40 回复(0)
//利用string对输入单词进行拼接
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string in, out;
    cin>>out;
    while (cin>>in)
    {
        out = in + " " + out;
    }
    cout<<out<<endl;

    system("pause");
    return 0;
}

发表于 2018-01-23 12:33:15 回复(12)

python one line solution:

print(" ".join(input().split()[::-1]))
发表于 2017-10-11 09:06:15 回复(2)
#include<bits/stdc++.h>
using namespace std;

int main(){
    char str[80];
    while(cin.getline(str,80)){
        const char* sep=" ";
        char* p=NULL;
        stack<char*> s;
        for(p=strtok(str,sep);p!=NULL;p=strtok(NULL,sep)){
            s.emplace(p);
        }
        while(!s.empty()){
            cout<<s.top()<<" ";
            s.pop();
        }
    }
    return 0;
}
发表于 2022-10-18 17:22:32 回复(5)
#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;
}

发表于 2021-03-04 20:35:42 回复(0)
string =raw_input(). split() string. reverse() print(' ' .join(string))
发表于 2020-08-26 10:26:38 回复(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;
}

发表于 2020-06-23 18:07:01 回复(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; 
 } 

发表于 2020-04-20 14:57:58 回复(0)

问题信息

难度:
70条回答 15892浏览

热门推荐

通过挑战的用户

说反话 (20)