从用户输入中搜索两个值-第二个值失败

Searching two values from user input - 2nd value fails

本文关键字:两个 失败 第二个 输入 用户 搜索      更新时间:2023-10-16

在代码中找不到错误。

我正试图在用户输入的句子中找到goodgreat

在搜索good的时候可以,但是搜索great的时候不行。

 #include<iostream>
    #include<fstream>
    using namespace std;
    bool read1();
    void write()
    {
        ofstream fout("sentData.txt",ios::out);
        char sentence[1000]={''};
        cout<<"Enter a sentence to check whether it contains "good" or "great" : "<<endl;
        cin.getline(sentence,1000);
        fout<<sentence;
    }
    int read()
    {
        ifstream fin("sentData.txt", ios::in);
        char sentence[100] = { '' };
        char good[5] = { 'g', 'o', 'o', 'd', '' };
        int count;
        bool check=0;
        while (!fin.eof())
        {
            fin.getline(sentence, 150);
            for (int i = 0; sentence[i] != ''; i++)
            {
                int k = 0;
                 count = 0;
    for (int j = i; sentence[j] != 32; j++)
            {
                if (sentence[j] == good[k])
                        {count++;
                        }
                    k++;
            }
            if(count==4)
               {
                  check=1;
                  break;
               }
        }
        if(count==4)
               {
                  break;
               }
    }
    fin.close();
    bool x=read1();
    if(check==1 || x==1)
        cout<<"Positive sentence."<<endl;
    }
    bool read1()
    {
        ifstream in("sentData.txt", ios::in);
        char sentence[100] = { '' };
        char great[6]={'g','r','e','a','t',''};
        int count;
    while(!in.eof())
    {
            in.getline(sentence,100);
                for (int i = 0; sentence[i] != ''; i++)
            {
                int k = 0;
                 count=0;
                for (int j = i; sentence[j] != 32; j++)
                {
                    if (sentence[j] == great[k])
                        {count++;
                        }
                    k++;
                }
                if(count==5)
                {
                    return 1;
                    break;
                }
            }
    }
    }

    int main()
    {
    write();
    read();
    return 0;
    }

在字符数组中搜索子字符串可能需要两次循环。一种更快的方法是在句子中搜索关键文本的第一个字母(例如:"好")。如果找到第一个字母,则搜索键的其余部分:

static const char sentence[]="No good nor great deed goes unpunished";
static const char key_text[] = "good";
const unsigned int sentence_length = strlen(sentence);
unsigned int index = 0;
// Outer loop, searching for first character of key text.
bool found = false;
for (unsigned int i = 0;(!found) && (i < sentence_length); ++i)
{
  if (sentence[i] == key_text[0])
  {
    // First character found now, examine remaining key text
    // Perform some preliminary checks, such as there must be
    //   enough letters remaining in the sentence.
    const unsigned int key_length = strlen(key_text);
    const unsigned int remaining_length = sentence_length - i;
    if (remaining_length >= key_length)
    {
      unsigned int k = 0;
      found = true;
      for (k = 0; k < key_length; ++k)
      {
        if (sentence[i + k] != key_text[k])
        {
           found = false;
           break; // Exit the "for" loop.
        }
      }
    }
  }
}
if (found)
{
  std::cout << "Key was found in sentence.n";
}
else
{
  std::cout << "Key not found in sentence.n";
}

一些笔记:
1. 在初始化数组时,不需要单独指定每个字符。
2. 对空间进行比较是没有意义的,因为对空间进行比较会增加复杂性。空格不能等于键中的第一个字符。
3.您可以通过以下方式加快搜索速度:length(sentence) - length(key_text)。
4. 还有更有效的方法,在网上搜索"c++高效子字符串搜索"应该会产生很多信息。
5. 此外,要养成习惯,对不会改变的变量使用const,比如关键文本。
6. bool类型使用truefalse。标识符truefalse是编译器识别的关键字,因此不需要定义它们。