implement recursive and iterative functions to reverse a string

This commit is contained in:
MarcUs7i 2025-01-11 00:11:49 +01:00
parent 775a442db9
commit 3f71d9418a

View file

@ -24,10 +24,24 @@ Note that strings that are stored on heap or stack are provided - they can be di
// TODO: the recursive implementation
void reverse_string_recursive(char* str, int start, int end) {
if(start >= end) {
return;
}
// TODO: the iterative implementation
void reverse_string_iterative(char* str, int start, int end) {
return;
char temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
reverse_string_recursive(str, start, end);
}
void reverse_string_iterative(char* str, int start, int end) {
while(start < end) {
char temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}