我在这里有这个程序来搜索并计算某个单词在文本文件中的字符串中出现的次数。
for(int i = 0; i < file_string_lenght - word_search_lenght; i++)
{
found = 1;
for(int j = 0; j < word_search_lenght; j++)
{
if(file_string[i + j] != word_search[j])
{
found = 0;
break;
}
}
if(found == 1)
{ //the question on the start of the program, whether user wants to print each found word's location
if(strcmp(response, "yes") == 0){
printf("'%s' found at index: %d \n", word_search, i);
}
count_of_word_search += 1;
}
}
if(pocet == 0){
printf("The word '%s' was not found in the file.", word_search);
printf("\n\n");
system("pause");
return 0;
}
printf("\nNumber of '%s' in the file: %d", word_search, count_of_word_search);
我怎么能这样做,例如,如果它在“今天”或“昨天”中,“天”这个词就不算数。我知道它需要识别我正在搜索的单词与字符串的其余部分之间的点、列或空格。但是我还没有找到一种方法来实现这种算法。感谢帮助