For this assignment, you will write two recursive functions, both of which will
ID: 3935860 • Letter: F
Question
For this assignment, you will write two recursive functions, both of which will parse any length string that consists of digits and numbers. Both functions should be in the same class and have the following signatures. Make sure you follow the UML exactly, as I will be running unit tests on your submission using this interface.
Write a recursive function that will add up the digits in the string. Example inputs and results:
2. Write a recursive function that will find the largest integer in this string.
input string result "1d2d3d" 6 "55" 10 "xx" 0Explanation / Answer
Answer:
Q. 1 Write a recursive function that will add up the digits in the string. Example inputs and results:
Answer:
#include<ctype.h>
int sum;
// char str[]="p13d3f";
void fun()
{
char ch;
scanf("%c", &ch);
if (isdigit(ch))
{
sum += ch - '0';
}
if (ch != ' ')
{
fun();
}
}
main()
{
printf("String.. ");
fun();
printf("Sum=%d", sum);
}
Q Write a recursive function that will find the largest integer in this string.
Answer:
#include<stdio.h>
#include<ctype.h>
int num,
large;
void fun()
{
char ch;
ch = getchar();
if (isdigit(ch))
{
num = ch - '0';
ch = getchar();
while (isdigit(ch))
{
num = num * 10 + ch - '0';
ch = getchar();
}
if (large <= num)
{
large = num;
}
}
if (ch != ' ')
fun();
}
main()
{
printf("Enter the string: ");
fun();
printf("largest value=%d", large);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.