I am working on a function to count the number of times that a string appears in an array. For example, if I had the following array:
Code:
char str[][10] = {"John","Mary","Bob","Dueche","John","john","Sam"};
I would like to find out how many times "John" appeared (I put "john" in the list to make sure that my function would determine the difference).
My desired result would be to create a frequency array that stores the frequency of the strings from the str array.
I have the following code below that accurately counts the frequency, however, it creates duplicate entries.
Code:
int hist(char StrAry[][MAX_SIZE], int l, int lines)
{
int i, k;
for(l=0; l<lines; l++)
{
printf("\n%s\t", StrAry[l]);
k = count(StrAry, l, lines);
for(i=0; i<k; i++)
printf("*");
}
printf("\n");
return 0;
}
int count(char StrAry[][MAX_SIZE], int l, int lines)
{
int i,j;
int k = 0;
for(i=0; i<lines; i++)
{
j = strcmp(StrAry[l], StrAry[i]);
if(j == 0)
{
k++;
}
}
return k;
}
Any ideas?
Thanks.