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
|
||||
void reverse_string_recursive(char* str, int start, int end) {
|
||||
if(start >= end) {
|
||||
return;
|
||||
}
|
||||
|
||||
char temp = str[start];
|
||||
str[start] = str[end];
|
||||
str[end] = temp;
|
||||
start++;
|
||||
end--;
|
||||
reverse_string_recursive(str, start, end);
|
||||
}
|
||||
|
||||
// TODO: the iterative implementation
|
||||
void reverse_string_iterative(char* str, int start, int end) {
|
||||
return;
|
||||
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