So I have to write a program counting the sum of digits, and the number of characters in a string... it looks like this. Oh, and it has to use corecursive functions, so that's why I used it if it seems inappropriate for this task.
Code:
/*Corecursive Functions 11/29/11 */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void count_alph(char * word, int i, int alphacount, int digitCount);
void sum_digit(char * word, int j, int alphacount, int digitCount);
int main()
{
char word[100];
/*String input prompt*/
printf("Input the word to be checked: \n");
scanf("%s", word);
count_alph(word,0,0,0,0);
}
void count_alph(char *word, int i, int alphacount, int digitCount)
{
while (i < strlen(word))
{
if (isalpha(word[i]))
{
alphacount++;
i++;
}
else
sum_digit(word, i, alphacount, digitCount);
}
printf("There are %d letters. ", alphacount);
}
void sum_digit(char *word, int j, int alphacount, int digitCount)
{
while (j < strlen(word))
{
if (isdigit(word[j]))
{
digitCount += (word[j] - '0');
j++;
}
else
count_alph(word, j, alphacount, digitCount);}
printf("The sum of the digits is %d. ", digitCount);
}
For some reason, when I input "b2b", it loops indefinitely, only printing "There are 2 letters." thousands of times. It's supposed to say: "There are two letters. The sum of the digits is 2."