strstr
/* finds the next instance of needle in haystack
zbpos: the zero-based position to begin searching from
haystack: the string to search in
needle: the string that must be found
returns the next match of `needle` in `haystack`, or -1 if not found
*/
int findnext(int zbpos, const char *haystack, const char *needle)
{
char *p;
if (((p = strstr(haystack + zbpos, needle)) != NULL)
return p - haystack;
return -1;
}
strstr
在 needle
(第一个)参数中搜索 needle
指向的字符串。如果找到,strstr
将返回事件的地址。如果找不到 needle
,则返回 NULL。我们使用 zbpos
,以便我们不会一遍又一遍地找到相同的针。为了跳过第一个实例,我们添加了 zbpos
的偏移量。记事本克隆可能会像这样调用 findnext
,以实现其查找下一个对话框:
/*
Called when the user clicks "Find Next"
doc: The text of the document to search
findwhat: The string to find
*/
void onfindnext(const char *doc, const char *findwhat)
{
static int i;
if ((i = findnext(i, doc, findwhat)) != -1)
/* select the text starting from i and ending at i + strlen(findwhat) */
else
/* display a message box saying "end of search" */
}