C语言 strstr 使用 c 始终返回 true 在字符串中查找单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17422217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:51:08  来源:igfitidea点击:

strstr to find a word in a string using c returning true all the time

cc-stringsturbo-c

提问by magicianiam

Good Day,

再会,

So I decided to go trough my C again and started making a simple search the word in a string.

所以我决定再次通过我的 C 并开始对字符串中的单词进行简单的搜索。

Here is my code:

这是我的代码:

#include<stdio.h>
#include<conio.h>
#include<string.h>

main(){
char word[100];
char sentence[100];

clrscr();
printf("Enter a word: ");
fgets(word, 100, stdin);
getch();
printf("The word is : %s", &word);
getch();
printf("\nEnter a sentence: ");
fgets(sentence, 100, stdin);
getch();
printf("The sentence is: %s",&sentence);
getch();
if(strstr(sentence,word) == 0){
  printf("word found!");
}
getch();
return 0;
}

The problem now is that whenever i try to search for the word in a string using strstr, it always returns word found. I also tried using strcmpbut that will only compare the first instance of the string and stops when a match is not found so its not really advisable if you want to do a word searching in a string problem.

现在的问题是,每当我尝试使用 搜索字符串中的单词时strstr,它总是返回word found。我也尝试过使用,strcmp但这只会比较字符串的第一个实例,并在找不到匹配项时停止,因此如果您想在字符串问题中进行单词搜索,这并不是真正可取的。

I haven't really made a program like this before, never needed one actually. So may I ask why is not working properly since according to its description strstrshould be to search for a word in a sentence or did I just misunderstood it.

我以前从未真正制作过这样的程序,实际上从未需要过。所以我可以问为什么不能正常工作,因为根据它的描述strstr应该是在一个句子中搜索一个词还是我只是误解了它。

Also if you have any comments on my program feel free to say so, so that I may become aware of my mistake.

另外,如果您对我的程序有任何意见,请随时提出,以便我意识到我的错误。

Thank you

谢谢

Example: word: dog
sentence: the dog is here
should return true

示例:单词:狗
句子:狗在这里
应该返回真

回答by alk

This line

这条线

if(strstr(sentence,word) == 0)

should be

应该

if(strstr(sentence,word) != NULL)

strstr()returns a pointer to the first occurence of wordin sentenceor NULLif the word was not found.

strstr()返回一个指向wordin第一次出现的指针,sentence或者NULL如果没有找到该词。

For details please read here.

详情请阅读此处



Also using fgets()to read in the "strings" appends a '\n'to the end of the "strings". This prohibits successful comparsion.

fgets()用于读入“字符串”,在“字符串”'\n'的末尾附加一个。这阻止了成功的比较。

To chop of the trailing '\n'you could do like this:

要截断尾随,'\n'您可以这样做:

fgets(word, sizeof(word), stdin);
{
  size_t size = strlen(word);

  if (size && ('\n' == word[size-1]))
  {
    word[size-1] = '
if(strstr(sentence,word) == 0){
  printf("word found!");
}
'; } }

回答by Karthik T

Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.

返回指向 str1 中第一次出现 str2 的指针,如果 str2 不是 str1 的一部分,则返回空指针。

Returning NULL (0) essentially means that the string was NOT found.

返回 NULL (0) 实质上意味着未找到该字符串。

Thus

因此

if(strstr(sentence,word) != NULL){
  printf("word found!");
}

Should be

应该

if(strstr(sentence,word) != NULL){
printf("word found");
}

Source - http://en.cppreference.com/w/c/string/byte/strstr

来源 - http://en.cppreference.com/w/c/string/byte/strstr

Additionally as Armin points out, you shouldn't use the address operator, &, in your printf. %sexpects to have a pointer passed to printfand using the name of the array as sentenceis actually the same as a pointer to the start of the array, i.e sentence == &sentence[0]

此外,正如 Armin 指出的那样,您不应&printf. %s期望有一个指针传递给printf并使用数组的名称,因为sentence它实际上与指向数组开头的指针相同,即sentence == &sentence[0]

回答by jinal shah

The strstr()function finds the first occurrence of the substring "needle" in the string "haystack" and it returns the pointer to the beginning of the substring or NULLif not found.

strstr()函数在字符串“haystack”中找到子字符串“needle”的第一次出现,NULL如果没有找到,则返回指向子字符串开头的指针。

so your if statement should be

所以你的 if 语句应该是

printf("%s",&word) 

Array name represents base address of its own, so writing

数组名代表它自己的基地址,所以写成

printf("%s",word) 

or

或者

if(strstr(sentence,word) == 0){
 printf("word found!");
}

both repesents base address of array.

两者都代表数组的基地址。

回答by Shubham Gupta

There is a little correction in your code i.e.,

您的代码中有一点更正,即,

#define NULL ((void *)0)

Change is: instead ==0 to !=0 which is equivalent to !=NULL, in case of pointers 0 and NULL are same. because NULL keyword is defined as:

变化是:在指针 0 和 NULL 相同的情况下,将 ==0 改为 !=0,相当于 !=NULL。因为 NULL 关键字定义为:

if(strstr(sentence,word) != 0){
 printf("word found!");
}

so change your code to

所以将您的代码更改为

##代码##