不跟你多bb,直接上代码
#pragma warning(disable:4996)
#include<stdio.h>
#include<string.h>
void reverseStr(char* left, char* right)
{
while (left < right)
{
char tmp = *left;
*left = *right;
*right = tmp;
left++;
right--;
}
}
void reverse(char* str)
{
int len = strlen(str);
if (len <= 1)
return;
reverseStr(str, str + len - 1);
char* cur = str;
while (*cur)
{
char* start = cur;
while (*cur != ' '&&*cur != '\0')
++cur;
//当前单词区间[start,cur)
reverseStr(start, cur - 1);
//查找下一个单词区间
while(*cur == ' ')
++cur;
}
}
void test()
{
char str[999] = { 0 };
gets(str);
reverse(str);
printf("%s\n", str);
}
int main()
{
test();
return 0;
}
这运行得没毛病,铁汁