У меня есть корректура вашего кода и комментарии к стилю кодировки и именам переменных. Там по-прежнему является недостатком, который я оставил с условным, что связано с тем, что он не повторяет предложение .
Вот ваш код, размеченный:
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;
}
Я удивлен, что ваша программа даже возвращается. Вы используете неинициализированный 'i', который никогда не увеличивается в вашем' while (предложение [i]! = '\ N') ', потому что ваш' i ++; 'выходит за пределы области цикла из-за отсутствия фигурных скобок. –
Также неясно, что такое w, как инициализируется частотный массив, а не то, что total4. Это не является хорошей демонстрацией проблемы. – catfood
w - это слово, взятое от пользователя. total4 - количество абзацев. – user2500540