假设我有以下字符串:
in the interior of the inside is an inner inn
我想搜索“in”的出现(经常出现“in”)。
在我的程序中,我使用了 strstr 来执行此操作,但它返回误报。它将返回:
- in the interior of the inside is an inner inn
- interior of the inside is an inner inn
- inside is an inner inn
- inner inn
- inn
因此,思考“in”出现5次,这显然不正确。
我应如何继续搜索以为单词“in”?
答案 0 :(得分:3)
搜索“in”;注意空格。然后考虑以“in”开头并以“in”结尾的句子的边缘情况。
答案 1 :(得分:2)
另一种方法是:
在整个句子中使用strtok()
,空格作为分隔符。
所以现在你可以针对“in”
检查你的令牌答案 2 :(得分:2)
尝试以下
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
char *s = "in the interior of the inside is an inner inn";
char *t = "in";
size_t n = strlen( t );
size_t count = 0;
char *p = s;
while ( ( p = strstr( p, t ) ) != NULL )
{
char *q = p + n;
if ( p == s || isblank( ( unsigned char ) *( p - 1 ) ) )
{
if ( *q == '\0' || isblank( ( unsigned char ) *q ) ) ++count;
}
p = q;
}
printf( "There are %zu string \"%s\"\n", count, t );
return 0;
}
输出
There are 1 string "in"
如果源字符串可以包含波动,您还可以添加ispunct
的检查。
答案 3 :(得分:1)
添加isdelimiter()
以检查strstr()
的前后结果。
// Adjust as needed.
int isdelimiter(char ch) {
return (ch == ' ') || (ch == '\0');
}
int MatchAlex(const char *haystack, const char *needle) {
int match = 0;
const char *h = haystack;
const char *m;
size_t len = strlen(needle);
while ((m = strstr(h, needle)) != NULL) {
if ((m == haystack || isdelimiter(m[-1])) && isdelimiter(m[len])) {
// printf("'%s'",m);
match++;
h += len;
} else {
h++;
}
}
return match;
}
int main(void) {
printf("%d\n",
MatchAlex("in the interior of the inside is an inner inn xxin", "in"));
return 0;
}