//题目:字符串中单词的逆转,即将单词出现的顺序进行逆转。如:Today is Friday!转为 !Friday is Today。
#include<iostream>
//#include<stdio.h>
//#include<string.h>
using namespace std;
void *reverse (char *be,char *ed)
{
if(be ==NULL || ed == NULL) return NULL;
while(be < ed)
{
char temp;
temp = *be;
*be = *ed;
*ed = temp;
be++;ed--;
}
}
char *ReverseSentence(char *str)
{
if(str == NULL) return NULL;
char *begin = str;
char *end = str;
while(*end !='\0')
++end;
end--;//去掉空格
reverse(begin, end);
begin=end=str;
while(*begin != '\0')
{
if(*begin == ' ')
{
begin++;
end++;
continue;
}
//反转单词
else if(*end == ' '||*end == '\0')
{
reverse(begin, --end);
begin =++end;
}
else
++end;
}
return str;
}
void main()
{
char str[]="Today is sunday!";//^_^
char *p = ReverseSentence(str);
while(*p != '\0')
{
cout<<*p++;
}
}
字符串反转
最新推荐文章于 2022-08-07 15:43:35 发布