#include <iostream>
using namespace std;
void reverseFixlen(char *str, int len){
char *p = str+len-1;
while (str < p)
{
char c = *str;
*str = *p;
*p = c;
++str;
--p;
}
}
void reverse(char *str){
reverseFixlen(str,strlen(str));
}
void reverseWordsInSentence(char *sen){
int len = strlen(sen);
reverseFixlen(sen,len);
char *p = sen;
char *str;
while (*p!='\0')
{
while (*p == ' '&& *p!='\0')
{
p++;
}
str = p;
while (*p != ' '&& *p!='\0')
{
p++;
}
reverseFixlen(str,p-str);
}
}
int main(){
char a[] = "hello world";
//reverse(a);
reverseWordsInSentence(a);
cout<<a;
system("pause");
return 0;
}
反转句子的词序,就是全部反一次,然后把每个词再返回来。
最新推荐文章于 2022-01-01 15:16:36 发布