给出"I miss you now"输出"now you miss I"

思路就是先整体翻转这个句子变为:

won uoy ssim I

任何以单词为单位进行翻转

now you miss I;

以上就是思想,具体代码如下:

// ReverseSentence2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;


void Reverse(char* pb , char* pe)	 //将某一段字符串翻转过来
{
	char *pl = pb , *pr = pe;

	while(pl!=pr&&pl&&pr)
	{
		char temp = *pl;	   //不能写成char * temp = pl;否则交换字符的操作失败
		*pl = *pr;
		*pr = temp;

		if(pl+1==pr)break;
		pl++;
		pr--;
	}

}
char* ReverseSentence(char* pData)
 {
	if(!pData)
		return NULL;
	char* pb=pData;
	char* pe=pData;
	while(pe && *pe!='\0')
		pe++;
	pe--;
	Reverse(pb,pe);//翻转整个单词
	pb = pe =pData;//指针重新指到句子的头
	while (pe && *pe!='\0')
	{
		while (*pe!='\0'&&pe && *pe!=' ')
			pe++;
		pe--;
		Reverse(pb,pe);
		++pe;
		while(*pe!='\0'&&*pe==' ')
			pe++;
		pb=pe;
	}
	return pData;
}


int _tmain(int argc, _TCHAR* argv[])
{
	/*char str[]="abcd";
	int len = sizeof(str)/sizeof(char);
	Reverse(str,str+len-2);
	cout<<str<<endl;*/
	char str[]="I miss you now";
	cout<<ReverseSentence(str)<<endl;
	system("pause");
	return 0;
}


程序输出:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值