在完成fgets之后,我们必须去掉换行符(\n)。
否则,search_string 将在末尾有它并且搜索将失败[除非该字符串出现在要搜索的字符串的 end]。
对于一行/缓冲区中的 [最多] 两个匹配,代码是“硬连线”的。这可以概括为使用循环计算任意数量的匹配。
这要求将searchPtr 初始化为s 并传递给strstr。
请注意,原始代码将 searchPtr 递增 1 以查找后续匹配项。
这比增加search_string 的长度慢。
但是,如果(例如)要搜索的字符串为:
,则增加字符串长度会产生不同的结果:
aa
而且,要在其中搜索的字符串是(例如):
aaaaaa
- 按字符串长度递增将产生 3 个匹配项。
- 增加一个字符将产生 5 个匹配项。
这是重构后的代码。
我添加了代码以允许 多个 搜索字符串和行进行搜索。它们之间用空行分隔。
我已经注释了:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 1000
// dosearch -- perform a single search
// RETURNS: 1=more to do, 0=EOF
int
dosearch(void)
{
char s[SIZE];
char search_string[SIZE];
char *searchPtr;
int match;
int moreflg;
do {
// get search string -- stop on EOF
moreflg = (fgets(search_string, SIZE, stdin) != NULL);
if (! moreflg)
break;
// strip newline and get string length
size_t len = strcspn(search_string,"\n");
search_string[len] = 0;
// show the [small] string we wish to search for
printf("\n");
printf("String to search for is: %s\n",search_string);
// skipping by one char is slow -- we could skip by the length
// of the search string but this would work differently with:
// search string: aa
// buffer: aaaaaa
// skipping by string length would produce 3 matches
// skipping by one char would produce 5 matches
// so, better to use 1
#if 1
len = 1;
#endif
while (1) {
// get line to search -- stop on EOF
moreflg = (fgets(s, SIZE, stdin) != NULL);
if (! moreflg)
break;
// strip newline
s[strcspn(s,"\n")] = 0;
// blank line means start new search -- caller will loop for us
if (s[0] == 0)
break;
printf("String to search within is: %s\n",s);
match = 0;
// point to start of line buffer
searchPtr = s;
while (1) {
// search for next occurence of string
searchPtr = strstr(searchPtr,search_string);
if (searchPtr == NULL)
break;
// increase the number of matches
++match;
printf("Match #%d found at: %s\n",match,searchPtr);
// skip over the match we just made
searchPtr += len;
}
printf("A match occurred %d times\n",match);
}
} while (0);
return moreflg;
}
int
main(void)
{
while (1) {
if (! dosearch())
break;
}
}
这里是一些示例输入数据:
world
hello world world
world is not enough
world
quick
the quick brown fox jumped over lazy dogs quickly
aa
aaaaaa
这是程序输出:
String to search for is: world
String to search within is: hello world world
Match #1 found at: world world
Match #2 found at: world
A match occurred 2 times
String to search within is: world is not enough
Match #1 found at: world is not enough
A match occurred 1 times
String to search within is: world
Match #1 found at: world
A match occurred 1 times
String to search for is: quick
String to search within is: the quick brown fox jumped over lazy dogs quickly
Match #1 found at: quick brown fox jumped over lazy dogs quickly
Match #2 found at: quickly
A match occurred 2 times
String to search for is: aa
String to search within is: aaaaaa
Match #1 found at: aaaaaa
Match #2 found at: aaaaa
Match #3 found at: aaaa
Match #4 found at: aaa
Match #5 found at: aa
A match occurred 5 times