ccorecursion

How to use corecursion in c?


I need help trying to find where to place the print statements in each function (alpha_count and sum_digits) so that they will only print once (at the end of the program).

Ex.
Number of characters: 8
Sum of digits: 19

As of right now they print each time the function has been called. Any ideas?

#include <stdio.h> 
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

//Prototypes
void count_alpha(char *s, int len, int index); 
void sum_digit(char *s, int len, int index);

#define SIZE 22

int main(void){
  //Declarations
  char string[SIZE];
  int length;

  //Get string from user
  printf("Enter a string of letters and numbers: ");
  scanf("%s", string);
  printf("String: %s\n", string);
  //Get length of string
  length = strlen(string);
  printf("Length: %d\n", length);
  //Get letters from string
  count_alpha(string, length, 0);

  return 0;
}

void count_alpha(char *s, int len, int index){
  static int characters = 0;
  char c = ' ';

  if (index < len){
    c = s[index];
    if(isalpha(c)){
        characters++;
        printf("char: %d\n", characters);
        index++;
        printf("index: %d\n", index);
        count_alpha(s, len, index);
    }
    else if(isdigit(c)){
        sum_digit(s, len, index);
    }
    //index++;
    //printf("index: %d\n", index);
    //printf("Number of Characters: %d\n", characters);
  }
  //else
  printf("Number of Characters: %d\n", characters);

}

void sum_digit(char *s, int len, int index){
  static int digits = 0;
  char c = ' ';

  if (index < len){
    c = s[index];
    if(isalpha(c)){
        count_alpha(s, len, index);
  }
  else if(isdigit(c)){
        printf("num is: %c", c);
        //printf("number is: %d", (atoi(&s[index])));
        //digits += atoi(&c);
        digits += c - '0';
        printf("sum: %d\n", digits);
        index++;
        printf("index: %d\n", index);
        sum_digit(s, len, index);
    }
    //index++;
    //printf("index: %d\n", index);
    //printf("Sum of digits: %d\n", digits);
  }
  //else
  printf("Sum of digits: %d\n", digits);

}

Solution

  • Declare int characters = 0 and int digits =0 globally, keeping them global will be of same use as of static variables in addition it can be accessed anywhere thus will help you in printing what you wanted just once in main function.

    For declaring them global just declare them outside all functions and at start of program.

    .
    .//header files
    .
    //#include <ctype.h>
    int characters = 0;
    int digits = 0;
    

    In main(): just print them

    printf("Number of Characters: %d\n", characters);
    printf("Sum of digits: %d\n", digits);
    

    EDIT: Without global variables by using pointers.

    void count_alpha(char *s, int len, int index,int *charac,int *dig);
    void sum_digit(char *s, int len, int index,int *charac,int *dig);
    

    In main:

    int* charac=&characters;
    int* dig=&digits;
    count_alpha(string,length,0,charac,dig);
    

    And whenever you see a function call pass these pointers:

    count_alpha(string,length,0,charac,dig);
    

    For incrementing values just use:

    (*charac)++;
    (*dig)++;