在句子中查找单词 [英] Finding words in sentences

查看:77
本文介绍了在句子中查找单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有无法完成的编程任务.这一部分杀了我.

I have a programming assignment I cannot finish. This one part is killing me.

接受用户的一些文字.接受需要搜索的字符串.您的程序应该打印出在文本中找到的字符串出现的次数以及找到模式的位置.查看以下示例输出:

Accept some text from the user. Accept the string that needs to be searched. Your program is supposed to print the number of occurrences of the string found within the text, and the position at which the pattern was found. Look at the following sample output:

样本输出:输入文字:叫我以实玛利.几年前-没关系要花多长时间-我的钱包里几乎没有钱,也没什么特别让我感兴趣的,我以为我会航行一点,看看世界的水域.这是我摆脱脾脏,调节血液循环的一种方式."

Sample Output: Enter text: "Call me Ishmael. Some years ago - never mind how long precisely - having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen, and regulating the circulation"

要搜索的字符串–几年前"

String to search – "Some years ago"

出现次数– 1

找到位置– 18

这是我的功能:

void getText()
{
    string itext, word;
    int position;
    bool done =  false;
    cout << "Enter some text" << endl;
    cin >> itext;
    cout << "Enter the word or phrase to wish to find" << endl;
    cin >> word;
    char text[itext.length()];
    char search[word.length()];

    for(int i = 0; i < itext.length(); i++)
    {
        for(int j = 0; j < word.length(); j++)
        {
            if(text[i] == search[j])
            {
                position = i;
                cout << position;
            }
        } 
     }
}

推荐答案

这可能会让您入门:(来自Knuth-Morris-Pratt算法的伪代码)

This might get you started: (pseudo code from the Knuth-Morris-Pratt algorithm)

algorithm kmp_search:
    input:
        an array of characters, S (the text to be searched)
        an array of characters, W (the word sought)
    output:
        an integer (the zero-based position in S at which W is found)

    define variables:
        an integer, m ← 0 (the beginning of the current match in S)
        an integer, i ← 0 (the position of the current character in W)
        an array of integers, T (the table, computed elsewhere)

    while m + i < length(S) do
        if W[i] = S[m + i] then
            if i = length(W) - 1 then
                return m
            let i ← i + 1
        else
            if T[i] > -1 then
                let m ← m + i - T[i], i ← T[i]
            else
                let i ← 0, m ← m + 1

    (if we reach here, we have searched all of S unsuccessfully)
    return the length of S

http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm

更简单,并使用c ++ std库:

Simpler and using c++ std library :

#include <string>
#include <vector>

int main ()
{

  std::string str ("There are two needles in this haystack with needles.");
  std::string str2 ("needle");

  // different member versions of find in the same order as above:
  std::size_t found = 0;
  int matches = 0;
  std::vector<size_t> positions;

  while( found = str.find(str2) != std::string::npos) {
      matches++;
      positions.push_back(found);
    }
}

这篇关于在句子中查找单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆