2:单词倒排
总时间限制: 1000ms 内存限制: 65536kB
描述
编写程序,读入一行英文(只包含字母和空格,单词间以单个空格分隔),将所有单词的顺序倒排并输出,依然以单个空格分隔。
输入
输入为一个字符串(字符串长度至多为100)。
输出
输出为按要求排序后的字符串。
样例输入
I am a student
样例输出
student a am I
代码
#include "pch.h"
#include <iostream>
#include<stdio.h>
#include<string>
using namespace std;
int main()
{
char a[101];
cin.getline(a,100);
int len,temp;
len = strlen(a);
temp = len;
for (int i = len - 1; i >= 0; i--)
{
if (a[i] == ' ')
{
for (int j = i + 1; j <= temp - 1; j++)
{
printf("%c", a[j]);
}
printf(" ");
temp = i;
}
}
for (int j = 0; j <= temp - 1; j++)
printf("%c", a[j]);
return 0;
}