单词翻转

题目描述  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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值