Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a recursive function that converts a C++ string into an integer. Assume th

ID: 3629300 • Letter: W

Question

Write a recursive function that converts a C++ string into an integer. Assume that the string passed has digits only (there is no unary + sign) and that the resulting value corresponds to a positive integer or to zero. As in the function above, do not allocate any extra memory (except for one or two local integer variables). Do not use built in library functions but you may write your own helper functions. Overload the function if you need extra parameters.
Examples
toInteger("753") returns the value 753
toInteger("1") returns 1
toInteger("005") returns 5

Explanation / Answer

int power(int k)

{

   int f=1;

for(int i=1;i<=k;i++)

f=f*10;

return f;

}

int rec(char a[],int i,int n)

{

   if(i<0)

return n;

n=n+ power(i)*a[i];

i--;

rec(a[], i, n);

}

 

We initially  call the function rec() from main() by passing the character array, i=last index of the array, n=0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote