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.
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")
Now the x.empty() will return false. So the control will call the function again as such f("EIDI").return x.empty()? x : f(x.substr(1)) + x[0];
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.
Assumptions: The substring method returns the substring from the index to the end of the string.
Regards,
Sahil.
--Smile, it makes people wonder what you are thinking.