You're out of free questions.

Upgrade now

Write a function that takes a string and reverses the letters in place.

In general, an in-place algorithm will require swapping elements.

We swap the first and last characters, then the second and second-to-last characters, and so on until we reach the middle.

void reverse(char *str) { size_t rightIndex, leftIndex = 0, strLength = strlen(str); if (strLength == 0) { return; } rightIndex = strLength - 1; while (leftIndex < rightIndex) { // swap characters char charAtLeftIndex = str[leftIndex]; str[leftIndex] = str[rightIndex]; str[rightIndex] = charAtLeftIndex; // move towards middle leftIndex++; rightIndex--; } }

time and space.

Reset editor

Powered by qualified.io

. . .