0

recursion in c++ char*

I have this code #include <iostream> using namespace std; void reverse(char *str) { if(*str == '\0') return; else { reverse(str+1); cout<<*str; } } int main() { char str[] = "C++ is fun"; cout<<"Original String: "<<str<<endl; cout<<"Reversed String: "; reverse(str); return 0; } can anybody explain me this part of the code? reverse(str+1); what does it do?

1st Feb 2020, 4:24 PM
Lilit
Lilit - avatar
1 Antwort
0
In the line reverse(str+1); the function reverse is recursively called with the pointer str pointing to the next character in the string. This effectively shifts the starting point of the string one position to the right in each recursive call, allowing the function to traverse through the string characters in reverse order during the recursive unwinding process. It's like unraveling a string in reverse, one character at a time, until the original order is reversed and printed.
14th Apr 2024, 6:20 AM
`ᴴᵗᵗየ
`ᴴᵗᵗየ - avatar