I have this code:
#include<stdio.h>
#include<string.h>
unsigned long hash(char *str)
{
unsigned long hash = 5381;
int c;
while ((c = *str++))
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
}
int main()
{
char s[50],ans[50];
int max_freq = 0,num=0,hash_value,i;
unsigned long int a[10000],b[10000];
FILE *fp = fopen("/Users/Brikman23/Desktop/input.txt","r"); //remember to specify correct path of input file..
if (fp==NULL)
{
printf("Error: Input File not found..");
return -1;
}
while(fscanf(fp,"%s",s)==1)
{
hash_value = hash(s);
for(i=0;i<num;i++)
if(a[i]==hash_value)
break;
if(i==num)
{
b[num]=1;
a[num]=hash_value;
if(b[num]>max_freq)
{
max_freq = b[num];
strcpy(ans,s);
}
num++;
}
else
{
b[i]++;
if(b[i]>max_freq)
{
max_freq = b[i];
strcpy(ans,s);
}
}
}
printf("The word with the highest number of appearances is: %s",ans);
printf("\nNumber of appearances: %d",max_freq);
return 0;
}
when I compile this with gcc, it does not give me an error but the output always gives me Abort trap:6 when instead it should be giving me:
The word with the highest number of appearances is:
Number of appearances:
I have looked at other posts but they have not been of help. Any help would be greatly appreciated.