Skip to content

Instantly share code, notes, and snippets.

@elleryq
Created July 7, 2014 03:15
Show Gist options
  • Save elleryq/2cd03bcca38900a78af5 to your computer and use it in GitHub Desktop.
Save elleryq/2cd03bcca38900a78af5 to your computer and use it in GitHub Desktop.
reverse a string in C
#include <stdlib.h>
#include <string.h>
#include <assert.h>
char* reverse(char* s) {
assert(s!=NULL);
char *r = (char*)strdup(s);
int len = strlen(s);
if(r==NULL) {
return NULL;
}
while(--len) {
*(r+len) = *s;
s++;
}
*r = *s;
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment