Write a recursive version of the function reverse(s), which reverses the string
s
in place.
Here is my solution:
void reverse(char a[], int i, int j) {
char tmp;
if(i >= j) {
return;
}
reverse(a, i + 1, j - 1);
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
This exercise is right after the presentation of the quicksort algorithm.
This is the signature of the quicksort function presented in the book:
void qsort(int v[], int left, int right)
This is why I added 2 new parameters to the functions - to narrow the size of the array.
There are 2 base cases:
- When
i == j
, son
is even - When
i > j
, son
is odd
n
represents the number of characters in string s
.