Question

Write a recursive function count_digits that counts all the digits in a string. Program: C

Write a recursive function count_digits that counts all the digits in a string.

Program: C

Homework Answers

Answer #1

CODE:

#include <stdio.h>
char string[100];

int count_digits(char str[],int n)
{
static int count=0,i=0;
char c;
if(i!=n)
{
c=str[i++];
if(c>='0' && c<='9')
{
           count++;
   count_digits(string,n);
    }
else
   count_digits(string,n);
  
}
else
{
return count;
}
}
int main()
{
int count=0;
int i;
int strlen=0;
printf("Enter a string to find digits: ");
scanf("%s",string);
for(i=0;string[i]!='\0';i++)
{
   strlen=strlen+1;
   }
count=count_digits(string,strlen);

printf("Total digits is %d",count);

return 0;
}

OUTPUT:

If you have any doubts please COMMENT....

If you understand the answer please give THUMBS UP....

Know the answer?
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
Similar Questions
Write a C++ recursive function that counts the number of nodes in a singly linked list....
Write a C++ recursive function that counts the number of nodes in a singly linked list. (a) Test your function using different singly linked lists. Include your code. (b) Write a recurrence relation that represents your algorithm. (c) Solve the recurrence relation using the iterating or recursive tree method to obtain the running time of the algorithm in Big-O notation.
C++ Write a recursive function that reverses the given input string. No loops allowed, only use...
C++ Write a recursive function that reverses the given input string. No loops allowed, only use recursive functions. Do not add more or change the parameters to the original function. Do not change the main program. I had asked this before but the solution I was given did not work. #include #include using namespace std; void reverse(string &str) { /*Code needed*/ } int main() {    string name = "sammy";    reverse(name);    cout << name << endl; //should display...
In C++ Write a program that will convert a string of binary digits to their decimal...
In C++ Write a program that will convert a string of binary digits to their decimal equivalent. For convenience limit the binary number to 16 bits. Write the decimal equivalent to the screen. For this program you must read in the binary number as a string type. If you were to enter a large binary number like 110110001100111 as a decimal value it would be too large to fit in an int type.
Program in C Write a function that replaces the contents of a string with the string...
Program in C Write a function that replaces the contents of a string with the string reversed.
Please code in Java Write a recursive method that takes a string and return a string...
Please code in Java Write a recursive method that takes a string and return a string where every character appears twice. For example, if the string is “HELLO”, it will return “HHEELLOO”. Write a program to test it.
1. Write a C++ program that implements the recursive function isMember as declared above. Pass an...
1. Write a C++ program that implements the recursive function isMember as declared above. Pass an array to the function by reference. Here are two sample runs of the program. The elements of the array are: 0 4 5 6 7 Enter the element you want to find: 6 Element is in the array Press any key to continue . . .
Write an 8088/8086 assembly program that counts the length of a null terminated string that starts...
Write an 8088/8086 assembly program that counts the length of a null terminated string that starts at location STR. Print the result on the screen.
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that calls this method from the Main program. Program should ask for input and be stored in the string and should return n amount of times.
Write a recursive function block_count(string) to return the number of blocks consisting of the same letter...
Write a recursive function block_count(string) to return the number of blocks consisting of the same letter in the given input string. Example. >>> block_count ('AABBCCBBDDD') 5 >>>block_count ('GGABAA') 4
Write a C program that counts the number of repeated characters in a phrase entered by...
Write a C program that counts the number of repeated characters in a phrase entered by the user and prints them. If none of the characters are repeated, then print “No character is repeated” For example: If the phrase is “full proof” then the output will be: Number of characters repeated: 3 Characters repeated: f, l, o Note: Assume the length of the string is 10.