Simvik

Reverse a string in place

Reverse the characters of a NUL-terminated string. An easy embedded C problem (Arrays and strings), run and graded in the browser.

easy · Arrays and strings · strings · two-pointers

Reverse the characters of s in place. The terminator stays at the end.

Starter code

void reverse_string(char *s) {
  // "abc" becomes "cba"; the '\0' stays where it is.
}

Example tests

Even length

char s[] = "embedded";
reverse_string(s);
printf("%s\n", s);

Expected output:

deddebme

Two characters

char s[] = "ab";
reverse_string(s);
printf("%s\n", s);

Expected output:

ba

Learn it first

  • Arrays and strings · Decay, bounds, safe copying, and parsing and formatting without the heap.

More Arrays and strings problems