Hi all,
I am trying to write a function that uses pointers to count the chars 'a', 'b', and 'c' in an input string.
heres my code. Thanks in advance
Code:
void count_abc(char *str, int *a, int *b, int *c) // a, b, c represents count for each letter
{
while (*str != '\0')
{
if (*str == 'a')
a++;
if (*str == 'b')
b++;
if (*str == 'c')
c++;
}
}
the main function is below
Code:
int main()
{
char str[MAX_STR];
int count_table[3] = {0,0,0};
int *a_count=&count_table[0],*b_count=&count_table[1],*c_count=&count_table[2];
printf("Enter a string of less than 50 characters:\n");
scanf("%s",str);
count_abc(str,a_count,b_count,c_count);
printf("number of a = %d , number of b = %d , number of c = %d\n",count_table[0],count_table[1],count_table[2]);
}