Strings In Depth14 / 18
Up next

Reverse in place

#include <stdio.h>
#include <string.h>

void reverse(char *s) {
    size_t i = 0, j = strlen(s);
    while (i < j) {
        j--;
        char tmp = s[i];
        s[i] = s[j];
        s[j] = tmp;
        i++;
    }
}

int main(void) {
    char s[] = "hello";
    reverse(s);
    printf("%s\n", s);
    return 0;
}
What you're solving

two-index swap toward the middle