So I have to write a programm to count how often every digit appears in a string. I've managed to make that work but now I want to be able to do this with more strings. So I need to reset the digits to zero and that function is not working...Here's my code:
#include <stdio.h>
void calc_occurrences(char s[], int occurrences[]);
void print_occurrences(int occurrences[]);
void reset_occurrences(int occurrences[]);
int main(int argc, char * argv[])
{
int parameters = argc - 1;
int occurrences[10] = {0};
static int i;
if (parameter_anzahl == 0) {
printf("Error, zero Parameters entered\n");
return 1;
}
for (i = 1; i <= argc - 1; i++) {
printf("\n\n");
printf("Parameter %i: %s\n", i, argv[i]);
calc_occurrences(argv[i], occurrences);
print_occurrences(occurrences);
reset_occurrences(occurrences);
}
return 0;
}
void calc_occurrences(char s[], int occurrences[])
{
int i = 0;
while (s[i]) {
if (s[i] >= '0' && s[i] <= '9') {
++occurrences[s[i] - '0'];
}
++i;
}
}
void print_occurrences(int occurrences[])
{
int k;
for (k = 0; k < 10; k++) {
printf("%i: %i\n", k, occurrences[k]);
}
}
void reset_occurrences(int occurrences[])
{
int i = 0;
while (occurrences[i]) {
occurrences[i] = 0;
i++;
}
}