Write a recursive function that should take as a parameter a C++ string of \'1\'
ID: 3759681 • Letter: W
Question
Write a recursive function that should take as a parameter a C++ string of '1's and '0's that are the binary representation of a positive integer, and return the equivalent int data type decimal value. Do not use any number base conversion functionality that is built into C++.
I need a correct answer. I posted this twice before, people gave me completely wrong or irrelevant answers here.
important notes:
-Function must have string parameter and return integer (so NO void is allowed)
-you are not allowed to use any loops when writing a recursive function. However, you are welcome to use helper functions.
-you are not allowed use stringstream.
Explanation / Answer
string reverse(string str)
{
string word = "";
if (str.length() <= 1)
{
return str;
}
else
{
string str_copy = str;
int n = str_copy.length() - 1;
string last_letter = str_copy.substr(n, 1);
str_copy = str_copy.substr(0, n);
word += reverse(str_copy);
return str_copy;
}
return word;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.