Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Which of the following statements are true about the two functions defined here? string reverse _ str ( string str ) { string val =

Which of the following statements are true about the two functions defined here?
string reverse_str(string str)
{
string val ="";
if (str ==""|| str.length()<=1)
{
return str;
}
string first = str.substr(0,1);
string rest = str.substr(1, str.length()-1);
val = reverse_str(rest).append(first);
return val;
}
string iter_reverse(string str)
{
int len = str.length();
int mid = len /2;
for (int i =0; i < mid; i++)
{
char ch = str[i];
int pos = len - i -1;
str[i]= str[pos];
str[pos]= ch;
}
return str;
}
Question 18 options:
The reverse function is a recursive solution, and the iter_reverse function is an iterative solution.
The two functions execute at about the same speed.
For a string of length n, the reverse function makes about n/2 recursive calls but the iter_reverse function performs about n/2 iterations.
All of the above.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Main Memory Database Systems

Authors: Frans Faerber, Alfons Kemper, Per-Åke Alfons

1st Edition

1680833243, 978-1680833249

More Books

Students also viewed these Databases questions