Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        4年前关闭。
                                                                                            
                
        
如何通过仅给定char *和长度作为输入参数并接收反转的字符串作为输出来反转C中的字符串?
同样,不使用strrev函数。
只是通过usign循环?

编辑:好的,那就是我现在的做法:

char*  __stdcall Reverse(char *str, int length, char* *out)
{

 char *s = str;

    char *end = s + length - 1;
    for (; s < end; ++s, --end) {
        char ch = *end;
        *end = *s;
        *s = ch;
    }
      *out = str;
}

最佳答案

#include <string.h>

char *strrev(char *str)
{
  char *p1, *p2;

  if (! str || ! *str)
        return str;
  for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
  {
        *p1 ^= *p2;
        *p2 ^= *p1;
        *p1 ^= *p2;
  }
  return str;
}

#include <stdio.h>

int main(int argc, char *argv[])
{
  while (--argc)
  {
        printf("\"%s\" backwards is ", *++argv);
        printf("\"%s\"\n", strrev(*argv));
  }
}


您可以实现自己的strrev(我从here那里获得了它)。试试c strrev source作为Google查询。

08-18 13:39