Write a recursive function count_digits that counts all the digits in a string.
Program: C
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....
Get Answers For Free
Most questions answered within 1 hours.