-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringReverse-rec.cpp
More file actions
40 lines (33 loc) · 1010 Bytes
/
StringReverse-rec.cpp
File metadata and controls
40 lines (33 loc) · 1010 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
Reversing a string is a common operation in text processing, and recursion offers an
elegant way to solve it. Your task is to write a recursive function in C++ that reverses
a given string without using loops or built-in reverse functions. The function should swap
the first and last character of the string, then recursively reverse the remaining substring.
For example, the input "HELLO" should produce the output "OLLEH". This task will help you
understand how recursion can be used to manipulate strings by focusing on base cases and
combining results from smaller subproblems.
*/
#include <iostream>
using namespace std;
void swap(char &a, char &b)
{
char temp = a;
a = b;
b = temp;
}
void reverse(string &str, int i = 0)
{
int n = str.length();
if (i >= n / 2)
return;
swap(str[i], str[n - 1 - i]);
reverse(str, i + 1);
}
int main()
{
string str = "HELLO";
cout << "Before reverse: " << str << endl;
reverse(str);
cout << "After reverse: " << str << endl;
return 0;
}