C++ 在句子中查找单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13565907/
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-08-27 17:29:24  来源:igfitidea点击:

C++ Find word in sentence

c++stringfindwords

提问by Tomá? Ptá?ek

I've got a task to find the word "EYE" in a sentence (more like just line of chars) such as: EYEYECARASDFG. As you can see, the word "EYE" is there twice, overlapping each other. I am suppose to couthow many times the word "EYE" occurs in the sentence. I wrote some code that looks like this:

我有一项任务是在句子中找到单词“EYE”(更像是一行字符),例如:EYEYECARASDFG。如您所见,“EYE”这个词出现了两次,彼此重叠。我想cout“EYE”这个词在句子中出现了多少次。我写了一些看起来像这样的代码:

#include <iostream>
#include <string>

using namespace std;

string sentence;

int main()
{
    int i = 0;
    cin >> sentence;

    while()
    {
        if (std::string::npos != sentence.find("EYE"))
        {
            i++;
        }
    }


    cout << i;
}

Now without the whileloop, it finds the EYE in the sentence and it kinda works. So I though, to count with the overlapping and make the code running until it hits the end, I need to loop it. So I though the while loop would be the best, but I don't know how to loop it, what to put into the brackets for whileloop

现在没有while循环,它会在句子中找到 EYE,它有点工作。所以我虽然要计算重叠并使代码运行直到它结束,我需要循环它。所以我虽然while循环是最好的,但我不知道如何循环它,在括号中放入什么forwhile循环

回答by janisz

First of all condition in whileis required. If you want infinite loop use trueas your statement. As a first draft, try to make it with "brute force". Just check every 3 letters substring of your sentence if equals "EYE". It will be one loop and 3 conditions or 2 loops and 1 condition. Then read about some text search algorithm e.g KMP.

首先条件 inwhile是必需的。如果你想无限循环true用作你的语句。作为初稿,尽量用“蛮力”来制作。如果等于“EYE”,只需检查句子的每 3 个字母子串。这将是一个循环和 3 个条件或 2 个循环和 1 个条件。然后阅读一些文本搜索算法,例如KMP

If you just want to make this code run use following coed:

如果您只想运行此代码,请使用以下 coed:

int pos = 0;
while(true) { 
    pos =  sentence.find("EYE", ++pos);
    if (pos != std::string::npos) {
        i++;
    } else break;
 }

回答by Jive Dadson

You can do this using a finite-state machine. (Google it.) That is efficient and easy to understand. As you read the characters, there are three states to distinguish, i.e., 1) when the most recent letter seen was an "E", 2) when the last two seen were "EY" in that order, and 3) everything else. As you go through a character at at time, increase the "found"-count by one whenever you are in state 2 and find another "E".

您可以使用有限状态机执行此操作。(谷歌它。)这是有效且易于理解的。当您阅读字符时,需要区分三种状态,即 1) 最近看到的字母是“E”,2) 看到的最后两个字母是“EY”,以及 3) 其他所有字母。当您在某个时间通过一个字符时,每当您处于状态 2 并找到另一个“E”时,将“找到”计数增加 1。

See if you can take it from there with no more hints.

看看你是否可以在没有更多提示的情况下从那里拿走它。

The idea can be extended to arbitrary strings besides "EYE", and you can write a compiler of sorts to generate the finite-state machines for those strings. But that is a more advanced assignment.

这个想法可以扩展到除“EYE”之外的任意字符串,您可以编写各种编译器来为这些字符串生成有限状态机。但这是一个更高级的任务。

回答by Rabiya

#include<iostream>
#include<string>
using namespace std;
main()
{
        string sen, sub;
        int pos;
        cout<<"Enter the Sentence"<<endl;
        getline(cin,sen);
        cout<<"Enter string to find"<<e`ndl;
        cin>>sub;
        for (int i=1;(pos=sen.find(sub)) != -1 ;i++)
        {
                sen=sen.substr(++pos);
                cout<<"Found = "<<sub<<" "<<i<<" Times"<<endl;
        }``


}

回答by rashedcs

Code Snippet :

代码片段:

#include <bits/stdc++.h>
using namespace std;



int main()
{
    string input, word;
    getline(cin, input);
    cin>>word;

    int cnt=0;
    size_t pos = input.find(word, 0);
    while(pos != string::npos)
    {
        cnt++;
        pos = input.find(word, pos+1);
    }
    cout<<cnt<<endl;

    return 0;
}


Input :
Python Programming Python
Python

Output : 2

输入:
Python 编程 Python
Python

输出:2