c - Frequency of each word of text file. Error while allocating memory? -
good evening everyone! have started messing around strings , pointers in c. want write programm reads text file, calculating frequency of each word , printing it.
my variables are:
file *fp; char *words[n] //n defined 100 int i=0, y=0; int *freq; int freq_count=0;; int word_number=0;
the code part:
for(i=0;i<word_counter;i++){ while(y<word_counter){ if(strcmp(words[i],words[y]==0){ freq1++; } y++; } if(i==0){ freq=(int*)malloc(sizeof(int)); strcpy(freq, freq1); freq1=0; } else{ freq=(int*)realloc(freq, (i+1)*sizeof(int)); strcpy(freq, freq1); freq1=0; } y=0; }
i several errors running this...what wrong? take consideration in words[n] have put each word of text in each cell.
thank in advance.
maybe array not want, still better using realloc
, condition in loop.
int freq[n]; for(i=0;i<word_counter;i++){ freq1 = 0; for(y=0;y<word_counter;y++){ if(strcmp(words[i],words[y]==0) freq1++; } freq[i] = freq1; }
Comments
Post a Comment