Help solve a problem regarding FUNCTION
What the followng function is doing:
string f(const string &x) {
return x.empty()? x : f(x.substr(1)) + x[0];
}
Guys - Please help solve this problem. Appreciate it.
Printable View
Help solve a problem regarding FUNCTION
What the followng function is doing:
string f(const string &x) {
return x.empty()? x : f(x.substr(1)) + x[0];
}
Guys - Please help solve this problem. Appreciate it.
Hi Heidi,
This is my analysis:
Take the string HEIDI.
The first time the function will be called with
f("HEIDI")
[quote] return x.empty()? x : f(x.substr(1)) + x[0];[/quote]Now the x.empty() will return false. So the control will call the function again as such f("EIDI").
The above statement can be read as:
return f("EIDI") + "H"
The next time it is called the statement becomes
return f("IDI") + "E"
and so on....
Esentially, this function will reverse the string passed to it.
[SIZE=1][I]Assumptions: The substring method returns the substring from the index to the end of the string.
[/I][SIZE=2]Regards,
Sahil.
[SIZE=1][COLOR=Silver]--Smile, it makes people wonder what you are thinking. :)[/COLOR][/SIZE]
[/SIZE][/SIZE]