Question

**C++ only and no vectors or global variables. Thank you, Write a recursive function to convert...

**C++ only and no vectors or global variables. Thank you,

Write a recursive function to convert a character string of digits to an integer. Example: convert("1234) returns 1234. Hint: To convert a character to a number, subtract the ASCII value '0' from the character, then the function can return the value s[0] - '0'.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>

#include<math.h>

using namespace std;

int string_to_int(string s)

{

// base case

if(s.size() == 1)

{

return s[0] - '0'; // if string is of length 1, return that digit

}

else

{

int n = s.size();

int k = (int)pow(10,n-1);

return (s[0] - '0')*k + string_to_int(s.substr(1,n-1)); // recursion part

}

}

int main()

{

cout << string_to_int("1234") << endl;

}

Add a comment
Know the answer?
Add Answer to:
**C++ only and no vectors or global variables. Thank you, Write a recursive function to convert...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a recursive function to convert a character string of digits to an integer. Example: convert(“1234”)...

    Write a recursive function to convert a character string of digits to an integer. Example: convert(“1234”) returns 1234. Hint: To convert a character to a number, subtract the ASCII value ‘0’ from the character. For example, if the string s has only one character, then the function can return the value s[0] – ‘0’.

  • c class ex in the 2pic Question 1: A. Write a recursive function Ecursive function that...

    c class ex in the 2pic Question 1: A. Write a recursive function Ecursive function that converts a string of digits into an integer. The function returns an integer that is converted from the original string 's'. int convertToint char *s); B. What's BIG-O notation of your function in part A. charx = "1234": Sở 3 : void printlnt (charts) if( *s == '() return; printf("%. Ch, ts): 7 printlnt (5+1): Ob..

  • Step 1: design and code a program that uses a recursive method to convert a String...

    Step 1: design and code a program that uses a recursive method to convert a String of digit characters into an integer variable. For example, convert the character string “13531” to an integer with the value 13,531. Note that the comma is present only to distinguish the integer value from the character string (in quotes) . The program should be repetitive to allow the user to enter any number of number strings, convert them, display them, and sum them. When...

  • C++ Write a recursive function that passes no parameters which returns the reverse of a global...

    C++ Write a recursive function that passes no parameters which returns the reverse of a global variable string str. It should be structured like this: string recursive() { return reversed_string; }

  • ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and...

    ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and including the given value. This function has to be recursive; you may not use loops! For example: Test Result print('%d : %d' % (5, sum_up_to(5))) 5 : 15 Q2, Write a recursive function that counts the number of odd integers in a given list. This function has to be recursive; you may not use loops! For example: Test Result print('%s : %d' % ([2,...

  • Arrays,Lists, Stacks and queues 1) Describe a recursive function to convert a string of digits into...

    Arrays,Lists, Stacks and queues 1) Describe a recursive function to convert a string of digits into the integer it represents. For example, the string “3735928559” would be converted to the integer value 3,735,928,559.[Preferrably in C++]

  • **C++ only using recursion, no vectors classes, or global variables *put #1 and #2 in the...

    **C++ only using recursion, no vectors classes, or global variables *put #1 and #2 in the same program and print using a recursive function 1) Write a function to fill an array of size 32 with values 1 to 32 recursively 2) Write a function to fill an array of size 32 with values 32 to 1 recursively

  • In C++ please. Thank you!! #include <iostream> #include <cstring> // print an array backwards, where 'first'...

    In C++ please. Thank you!! #include <iostream> #include <cstring> // print an array backwards, where 'first' is the first index // of the array, and 'last' is the last index void writeArrayBackward(const char anArray[], int first, int last) { int i = 0; for (i = last; i >= first; i--) { std::cout << anArray[i]; } std::cout << std::endl; } // test driver int main() { const char *s = "abc123"; writeArrayBackward(s, 0, strlen(s) - 1); } // Using the...

  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

  • PYTHON 3.6 (Short and Simple Codes Please) Write a recursive function, reverse, that accepts a parameter...

    PYTHON 3.6 (Short and Simple Codes Please) Write a recursive function, reverse, that accepts a parameter containing a string value and returns the original string in reverse. For example, calling reverse('goodbye') would return 'eybdoog'. Reversing a string involves: No action if the string is empty or only has 1 character Concatenating the last character with the result of reversing the string consisting of the second through next-to-last character, followed by the first character

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT