例如:
输入:s = “i am from beijing"
输出:”beijing form am i“
#include <stdio.h>
#include <string.h>
//i am form nanjing
//ginjnan mrof ma i
//nanjing form am i
//逆序算法(首尾互换)
void Reserve(char *s,int len)
{
for(int i = 0;i < len / 2;i++)
{
char temp = s[i];
s[i] = s[len - 1 - i];
s[len - 1 - i] = temp;
}
}
void Reserve2(char *s,int len)
{
int count = 0;
for(int i = 0;i < len;i++)
{
if(s[i] == ' ' || s[i] == '\0')
{
Reserve(s + i - count,count);
count = 0;
}
else
{
count++;
}
}
}
int main()
{
char str[] = "i am form nanjing";
Reserve(str,strlen(str));
Reserve2(str,strlen(str));
printf("%s\n",str);
return 0;
}