给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。
输入格式:
测试输入包含一个测试用例,在一行内给出总长度不超过 80 的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用 1 个空格分开,输入保证句子末尾没有多余的空格。
输出格式:
每个测试用例的输出占一行,输出倒序后的句子。
输入样例:
Hello World Here I Come
输出样例:
Come I Here World Hello
1、想用string实现二维数组,但结果却不对,明明string ans[i][j]里面放的字母是对的,但是想用cout<<ans[i]输出整个字符串却无法输出,最后没办法只能用字符数组了。
#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
int main(void)
{
char ans[85][85];
string str;
int words_count = 0;
getline(cin, str);
for(int i = 0; i < str.size();)
{
int letter_count = 0;
while(str[i] != ' ' && i < str.size())
{
ans[words_count][letter_count++] = str[i++];
if(str[i] == ' ')
{
i++;
ans[words_count][letter_count] = '\0';
words_count++;
break;
}
}
}
for(int i = words_count; i >= 0; i--)
{
cout<<ans[i];
if(i > 0)
{
cout<<' ';
}
}
return 0;
}