单词翻转--字符串

版权声明:转载请附上地址 https://blog.csdn.net/weixin_44574520/article/details/87468423

单词翻转

Codevs天梯

题目描述 Description
给出一个英语句子,希望你把句子里的单词顺序都翻转过来
输入描述 Input Description
输入包括一个英语句子。
输出描述 Output Description
按单词的顺序把单词倒序输出
样例输入 Sample Input
I love you
样例输出 Sample Output
you love I
数据范围及提示 Data Size & Hint
简单的字符串操作

题意分析:

  • 读一行用gets!
  • 模拟倒着输出

Code:

#include <bits/stdc++.h>
using namespace std;
int main(){
	freopen("pud.txt","r",stdin);
	char s[10000];
	gets(s);
	int last=strlen(s)-1,bj=0,size=1;
	for(int i=0;i<=last;i++){
		if(s[i]==' ') size++;
	}
    for(int i=last;i>=0;i--){
		if(s[i]==' '){
			for(int j=i+1;j<=last;j++){
				printf("%c",s[j]);
			}
			printf(" ");
			last=i-1;
			bj++;
			continue;
		}
    	if(bj==(size-1)){
    		for(int j=0;j<=last;j++) printf("%c",s[j]);
    		exit(0);
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44574520/article/details/87468423
今日推荐