题目描述
Description
给出一个英语句子,希望你把句子里的单词顺序都翻转过来
输入描述
Input Description
输入包括一个英语句子。
输出描述
Output Description
按单词的顺序把单词倒序输出
样例输入
Sample Input
I love you
样例输出
Sample Output
you love I
数据范围及提示
Data Size & Hint
简单的字符串操作
题解:
#include <cstdio>
#include <iostream>
#include <string.h>
#include <cstring>
using namespace std;
//单个字符串翻转
void reverse(char * arr,int start,int end){
while(start < end){
char temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
int main(){
int num = 0;
char a[100];
gets(a);
int n = strlen(a);
for(int i = 0;i<=n;i++){
if(a[i] == ' ' || a[i] == '\0'){
reverse(a,i-num,i-1);
num = 0;
}
else{
num ++;
}
}
for(int i = n-1;i>=0;i--){
cout<<a[i];
}
cout<<endl;
return 0;
}