【发布时间】:2020-05-31 08:32:39
【问题描述】:
我正在从文件中读取一堆行并尝试打印包含特定关键字集的每一行。现在我的代码只查找一行中第一次出现的关键字,如果找到,它将打印该关键字和该行及其行号。但我正在努力做到这一点,如果该行包含不止一次出现的关键字,那么在打印输出中,行号旁边应该有一个星号。我尝试了许多不同的方法,但似乎没有任何效果。这是我的代码:
for(i = 0; i < wordCount_keyword; i++) {
for(j = 0; j <= lineCount; j++) {
if(strstr(inputLines[j], keywords[i]) != NULL) {
printf("%-*s %s (%d)\n", max_length + 1, keywords_upper[i], inputLines[j], j+1);
}
}
}
这是我当前的输出:
CAT the fish a dog cat dog rabbit (1)
CAT the fish and cat (2)
DOG the fish a dog cat dog rabbit (1)
ELEPHANT a rabbit or elephant (3)
FISH the fish a dog cat dog rabbit (1)
FISH the fish and cat (2)
RABBIT the fish a dog cat dog rabbit (1)
RABBIT a rabbit or elephant (3)
这是我想要的理想正确输出:
CAT the fish a dog cat dog rabbit (1)
CAT the fish and cat (2)
DOG the fish a dog cat dog rabbit (1*)
ELEPHANT a rabbit or elephant (3)
FISH the fish a dog cat dog rabbit (1)
FISH the fish and cat (2)
RABBIT the fish a dog cat dog rabbit (1)
RABBIT a rabbit or elephant (3)
请注意区别在于第 3 行末尾括号中的 1 旁边的星号。 这样做的正确方法是什么?
【问题讨论】:
标签: c arrays string char printf