Reverse String

void reverse_string(char *str)
{
    if (!str)
        return;
 
    int n = strlen(str);
    char *c, *tmp;
 
    c = str;
    reverse(str, str + n - 1);
 
    while (1) {
        for (; *c == ' ' && *c != '\0'; c++);
        if (*c == '\0')
            return;
        tmp = c;
 
        for (; *c != ' ' && *c != '\0'; c++);
        reverse(tmp, c - 1);
        if (*c == '\0')
            return;
    }
}
 
void reverse(char *c1, char *c2)
{
    while (c2 > c1) {
        char tmp = *c1;
        *c1++ = *c2;
        *c2-- = tmp;
    }
}

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License