Question

Write a recursive function Programming: to_number that forms the integer sum of all digit characters in...

Write a recursive function Programming: to_number that forms the integer sum of all digit characters in a string. For example, the result of to_number("3ac4") would be 7.

C++

i know this has been answered before but I keep getting 0 when I enter a string.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <string>

using namespace std;

int to_number(string s) {
    if (s.length() == 0) {
        return 0;
    } else {
        int num = to_number(s.substr(1));
        if (s[0] >= '0' && s[0] <= '9')
            num += s[0] - '0';
        return num;
    }
}

int main() {
    string s;
    cout << "Enter a string: ";
    cin >> s;
    cout << to_number(s) << endl;
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write a recursive function Programming: to_number that forms the integer sum of all digit characters in...
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
  • 1. DOES A DIGIT APPEAR IN AN INTEGER, Write a recursive function appears(n,i) that returns true...

    1. DOES A DIGIT APPEAR IN AN INTEGER, Write a recursive function appears(n,i) that returns true if the digit i appears in the positive (base 10) integer n, false if it does not in c++. ex. Enter a positive integer: 54321 Enter a digit: 7 The digit 7 does NOT appear in the integer 54321. 2. IS AN INPUT STRING A PALINDROME? A palindrome is a string in which the letters are the same in both directions, disregarding capitalization and...

  • ****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,...

  • PYTHON: (Sum the digits in an integer using recursion) Write a recursive function that computes the...

    PYTHON: (Sum the digits in an integer using recursion) Write a recursive function that computes the sum of the digits in an integer. Use the following function header: def sumDigits(n): For example, sumDigits(234) returns 9. Write a test program that prompts the user to enter an integer and displays the sum of its digits. Sample Run Enter an integer: 231498 The sum of digits in 231498 is 27

  • Write a recursive function called sumover that has one argument n, which is an unsigned integer....

    Write a recursive function called sumover that has one argument n, which is an unsigned integer. The function returns a double value, which is the sum of the reciprocals of the first n positive integers. (The reciprocal of x is the fraction 1/x.) For example, sumover(1) returns 1.0 (which is 1/1); sumover(2) returns 1.5 (which is 1/1 + 1/2); sumover(3) returns approximately 1.833 (which is 1/1 + 1/2 + 1/3). Define sumover(0) to be zero. Do not use any local...

  • 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...

  • Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9.

     4.18 LAB: Checker for integer string Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9. Exx If the input is: 1995 the output is: yes Ex If the input is: 42,000 1995! the output is no Hint: Use a loop and the Character isDigitO function. 418.1: LAB: Checker for integer string

  • Use C++ to write a recursive function that takes in a non-negative integer and returns the...

    Use C++ to write a recursive function that takes in a non-negative integer and returns the count of the occurrences of 7 as a digit, so for example 717 yields 2. Note: Even though it would be easy to solve this problem iteratively, the goal is to get some practice solving problems recursively.

  • need help with python ( no loops used please!) Write a recursive function named sum_digits that...

    need help with python ( no loops used please!) Write a recursive function named sum_digits that takes a given a non-negative integer N and returns the sum of its digits. Hint: Mod (%) by 10 gives you the rightmost digit (126 % 10 is 6), while doing integer division by 10 removes the rightmost digit (126/10 is 12). This function has to be recursive; you are not allowed to use loops to solve this problem! This function takes in one...

  • Write a function that, given a word and a set of characters, filters all occurrences of...

    Write a function that, given a word and a set of characters, filters all occurrences of those characters from the word. For e.g. remove_chars("abcdeeeeefmnop", "ebo") returns: acdfmnp i.e all occurrences of e, b and o have been removed from abcdeeeeefmnop You should acquire the word and set of characters both from the user and then pass them to the function. Note: std::cin terminates when it encounters a space, you should take the word and character set as separate inputs: std::cin...

  • 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’.

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