-1

Can't find the bug in the code.

I am trying to find the words good or great in a sentence entered by a user.

It works fine when searching for good but does not work for great.

 #include<iostream>
    #include<fstream>

    using namespace std;
    bool read1();
    void write()
    {
        ofstream fout("sentData.txt",ios::out);

        char sentence[1000]={'\0'};

        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] = { '\0' };
        char good[5] = { 'g', 'o', 'o', 'd', '\0' };
        int count;
        bool check=0;

        while (!fin.eof())
        {
            fin.getline(sentence, 150);

            for (int i = 0; sentence[i] != '\0'; 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] = { '\0' };
        char great[6]={'g','r','e','a','t','\0'};

        int count;

    while(!in.eof())
    {
            in.getline(sentence,100);

                for (int i = 0; sentence[i] != '\0'; 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;
    }
t1f
  • 2,546
  • 3
  • 22
  • 47
Z ee
  • 3
  • 4

1 Answers1

1

Searching character arrays for a substring may take two loops. A faster method is to search the sentence for the first letter of the key text (e.g. "good"). If the first letter is found, then search for the remainder of the key:

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";
}

Some notes:
1. You don't need to specify each character separately when initializing an array.
2. There is no point in comparing for spaces, since comparing for spaces adds more complexity. A space will not be equal to the first character in the key.
3. You could speed up the search by only going as far as: length(sentence) - length(key_text).
4. There are more efficient methods, a search of the internet for "c++ efficient substring search" should produce a lot of information.
5. Also, get in the habit of using const for variables that won't change, such as your key text.
6. The bool type uses true or false. The identifiers true and false are keywords recognized by the compiler, so you don't need to define them.

Thomas Matthews
  • 52,985
  • 12
  • 85
  • 144