implement recursive and iterative functions to reverse a string
This commit is contained in:
parent
775a442db9
commit
3f71d9418a
1 changed files with 17 additions and 3 deletions
|
|
@ -24,10 +24,24 @@ Note that strings that are stored on heap or stack are provided - they can be di
|
||||||
|
|
||||||
// TODO: the recursive implementation
|
// TODO: the recursive implementation
|
||||||
void reverse_string_recursive(char* str, int start, int end) {
|
void reverse_string_recursive(char* str, int start, int end) {
|
||||||
|
if(start >= end) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: the iterative implementation
|
char temp = str[start];
|
||||||
void reverse_string_iterative(char* str, int start, int end) {
|
str[start] = str[end];
|
||||||
return;
|
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--;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue