Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C programming code that counts the number of digits in strings and sums the char

ID: 3936564 • Letter: C

Question

C programming code that counts the number of digits in strings and sums the characters. Problem 2 A function that calls another function, which in turn calls the original function, is said to be corecursive. Note that corecursive functions occur in pairs. Write a program that counts the number of digits in a string and sums the characters (ASCII code) in the string. For example, the string "Ai444apple7" has five digits, and the characters in the string sum to 599. Write a pair of corecursive functions to help carry out the tasks. Use count dig0 to count the alphabetic characters, and use sum_char for summing the digits. These two functions should call each other. For comparison, write a non-corecursive function that performs the two tasks in a direct, more natural fashion. Hint: If necessary, use static variables.

Explanation / Answer

//C programming code that counts the number of digits in strings and sums the characters.

#include <stdio.h>
void main()
{
char string[80];
int countdigit,sum = 0,len;

printf("Enter the string containing both digits and alphabet ");
scanf("%s", string);
len = strlen(string);
countdigit = count_digit(string);
sum = sumCharacter(string, 0)
printf("NO. of Digits in the string = %d ", nc);
printf("The sum of all ascii values in '%s' is %d. ", string, sum);
}

int sumCharacter(char str[], int num)
{
if (num < strlen(str))
{
return (str[num] + sumascii(str, num + 1));
}

return 0;
}

int count_digit(char str[]){
int count, nc = 0;
for (count = 0; string[count] != ''; count++)
{
if ((string[count] >= '0') && (string[count] <= '9'))
{
nc += 1;
}
}
return nc;
}

Thank you.