开发者

Shortest C code to reverse a string [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

Not counting the function signature (just the body) can anybody produce C code shorter than this function that will reverse a string and return the result as a pointer to the 开发者_StackOverflowreversed string.. (not using a string reverse library function either)?

char * reverse_str(char * s)
{
   char c,*f=s,*p=s;while(*p)p++;while(--p>s){c=*p;*p=*s;*s++=c;}return f;
}


not much longer, but it works.

#include <string.h>

/* precondition: s!=const && s!=NULL && *s!='\0' */
char *mystrrev(char *s)
{
  char *a=s,*e=s+strlen(s)-1;
  while( a<e )
  {
    char c=*a;
    *a++=*e;
    *e--=c;
  }
  return s;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜