把这个字符串God Skin 转换成 doG nikS 有没有会写这个简单的算法的
#include <stdio.h>
#include <ctype.h>
void swap(char *p, char *q)
{
char t = *p;
*p = *q;
*q = t;
}
void reverse_impl(char *first, char *last)
{
while (first < last)
swap(first++, --last);
}
void reverse(char *s)
{
while (*s)
{
char *p = s;
while (*p && isblank(*p))
p++;
char *q = p;
while (*q && !isblank(*q))
q++;
reverse_impl(p, q);
s = q;
}
}
int main(int argc, char *agv[])
{
char s[] = "God Skin";
reverse(s);
printf("%s\n", s);
return 0;
}
$ gcc -Wall main.c
$ ./a.out
doG nikS