不开辟用于交换数据的临时空间,如何完成字符串的逆序(在技术一轮面试中,有些面试官会这样问)

#include <stdio.h>

#include <string.h>

 

void reverse(char *s)

{

      int i = 0;

      int len = strlen(s);

      int j = len-1;

      while(i < j)

      {

            s[i] ^= s[j] ^= s[i] ^= s[j];

/*          s[i] = s[i]^s[j];

            s[j] = s[i]^s[j];

            s[i] = s[i]^s[j];*/

            i++;

            j--;

      }

 

}

 

int main(void)

{

      char str[] = "HelloWorld";

      printf("%s\n",str);

      reverse(str);

      printf("%s\n",str);

      return 0;

}

posted @ 2014-03-03 11:08  kin2321  阅读(318)  评论(0编辑  收藏  举报