Thread: Help on sum digits in C

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    5

    Help on sum digits in C

    Problem:
    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
    alphabetic characters in a string and sums the digits in the string. For example, the string
    "AOis444apple7" has eight alphabetic characters, and the digits in the string sum to 19. Write a pair of
    corecursive functions to help carry out the tasks. Use count_alph() to count the alphabetic characters,
    and use sum_digit() 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.
    Grading

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<ctype.h>
    
    
    int countAlpha(char *);// counts all alpha chars
    int sumDigits(char *);// finds digits and sums
    
    int main(void){
    
      char s[]=("AOis444apple7");  //string being tested
    	countAlpha(s);// calls string to alpha count
    
    
    }      
    int countAlpha(char *s)
    {
    int i;//counter
    int countAlpha=0;
    for(i=0;i<13;i++) {//checks all chars strlen of s
        if(isalpha(s[i])){//if it is an alpha char 
          countAlpha++;}//it adds 1 to count alpha
    		}
    printf("There are %d letters.",                      //alpha total
    					countAlpha);
    
    sumDigits(s);//calls to sumdigits function in count alpha
    
    return countAlpha;
     }
    
    int sumDigits(char *s)
    {
    int sum=0;
    int total=0;
    int i;
    
    for(i=0;i<13;i++) {//tests string finds digits
    	total=sum+(isdigit(*s));// finds the digit but for some reason cant pull value of that point to sum 
    	s++;
    	
     		}
    printf("and the sum of the digits is %d\n",
    						total);
    
    return sumDigits;
    }
    Im having trouble finding the sum of the digits after finding the digits in the string. My friend in class said using atoi, but i was wondering if there was a simpler solution? Didnt mean to make it sound like im dumping the problem and waiting for someone to solve it. Im just looking for advice.
    Last edited by KevinH123; 12-06-2010 at 08:44 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So rather than just dumping your assignment on us, how about you actually tell us what it is you are having problems with? Oh, and use code tags.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    I hope that helps....

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    What is this? Look at this again.

    Code:
    return sumDigits;

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    for(i=0;i<13;i++) {
    	total=sum+(isdigit(*s));
    	s++;
    }
    What is the returned value from isdigit()?
    How does that help you?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    Quote Originally Posted by CommonTater View Post
    Code:
    for(i=0;i<13;i++) {
    	total=sum+(isdigit(*s));
    	s++;
    }
    What is the returned value from isdigit()?
    How does that help you?
    How would i get the actual value from the point on the string thats a digit. This is something i found online. I had it before like

    Code:
    void sumDigits(char *s)
    {
    int sum=0;
    int total=0;
    int i;
    
    	for(i=0;i<13;i++) {			//tests string finds digits
    		if(isdigit(s[i])){          // checks if value is digit
    		total=sum+(s[i]);}
     			}
    				
    printf("and the sum of the digits is %d\n",
    						total);
    
    return;
    }

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok, so s[i] is a digit... but it's an ascii digit and will have a numerical value between 48 and 57.

    Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion

    Before you can use it mathematically you need to convert it to a decimal number. Thus your code has to be something like...
    Code:
    int total = 0;
    
    	for(i=0;i<13;i++) {		//tests string finds digits
    		if(isdigit(s[i])){          // checks if value is digit
    		total += s[i] - '0';}    // convert to short int
     			}

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How is it you think you're going to be summing up both letters and characters in two different variables, but you're only returning one?


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Total sum of digits
    By Veneficvs in forum C++ Programming
    Replies: 11
    Last Post: 05-30-2010, 11:58 AM
  2. How can i count sum of digits in odd/even places?
    By cowa in forum C Programming
    Replies: 9
    Last Post: 11-16-2009, 11:43 PM
  3. Minor Problem
    By stewie1986 in forum C Programming
    Replies: 6
    Last Post: 11-30-2007, 08:40 AM
  4. The sum of squared digits
    By wiz23 in forum C++ Programming
    Replies: 4
    Last Post: 04-10-2005, 09:05 AM
  5. Replies: 7
    Last Post: 05-26-2003, 05:44 PM