将"I am your friend"转为"friend your am I"。
#include <iostream>
#include <stdlib.h>
void reverse(char* pBegin, char* pEnd)//反转任意的字符串
{
if(pBegin == NULL || pEnd == NULL)
{
return;
}
while(pBegin < pEnd)
{
char temp=*pBegin;
*pBegin=*pEnd;
*pEnd=temp;
pBegin++;
pEnd--;
}
return;
}
void reverse_sentence(char* begin)
{
if(begin == NULL)
{
return;
}
char* end=begin;
while(*end != '\0')
{
end++;
}
end--;
reverse(begin,end);//先反转整句话
end=begin;
/*开始逐个反转单词*/
while(*begin != '\0')
{
if(*begin == ' ')//跳过空格
{
begin++,end++;
continue;
}
else if(*end == ' ' || *end == '\0')
{