私はあなたのコードを校正し、コーディング スタイルと変数名についてコメントしました。文を繰り返し処理していないために、条件文にまだ欠陥があります。
マークアップされたコードは次のとおりです。
while(fgets(sentence, sizeof sentence, cfPtr)) {
for(j=0;j<total4;j++){
frequency[j] = comparision(sentence,&w);
all_frequency+=frequency[j];
}
}
// int comparision(const char sentence[ ],char *w) w is a poor variable name in this case.
int comparison(const char sentence[ ], char *word) //word is a better name.
{
//int length=0,count=0,l=0,i;
//Each variable should get its own line.
//Also, i should be initialized and l is redundant.
//Here are properly initialized variables:
int length = 0;
int count = 0;
int i = 0;
//length= strlen(sentence); This is redundant, as you know that the line ends at '\n'
length = strlen(word); //l is replaced with length.
//while(sentence[i]!= '\n')
//The incrementor and the if statement should be stored inside of a block
//(Formal name for curley braces).
while(sentence[i] != '\n'){
if(strncmp(sentence, word, length) == 0) //strncmp returns 0 if equal, so you
count++; //should compare to 0 for equality
i++;
}
return count;
}