elleryq
7/7/2014 - 3:15 AM

reverse a string in C

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;
}