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

int main()
{
    int n, p = 0;
    string x, y = "yes";
    int k = x.size();
    int l = y.size();

    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> x;
    }

    for (int i = 0; i < k; i++) {
        for (int j = 0; j < l; j++) {
            if (x[i] == y[j]) {
                p++;
            }
        }
    }
    cout << p << endl;
}

I want to print the first line containing "yes" as substring. The numbering of lines starts at 1. For Example I have data like this:

4
noway
ofcourseyousilly
thisisthegayestquestion
iwouldneversayyes

and the output is 3, because 3rd line is the first one which contain yes as a substring. Can someone find the bug in my code?

mch
  • 9,424
  • 2
  • 28
  • 42
  • 1
    Take care with the tagging. Bringing in the C programmers brings in extra eyes and there is a lot of cross over between the C and C++ programmers, but any C programmers interested in fielding C++ questions would also be monitoring the C++ tag. If they aren't, misusing the C tag is more likely to be an annoyance. – user4581301 Apr 19 '22 at 20:31
  • 1
    using [strstr](https://en.cppreference.com/w/cpp/string/byte/strstr) should work, but probably a better CPlusPlussy way to do it. And either your line numbers start at 0 or it's found on the 4th line. – yano Apr 19 '22 at 20:33
  • What happens to the first 3 strings you read? Not sure you didn't notice the fact that those values are droped because of the abysmal formatting of the code? Basically every IDE provides functionality for automatically formating the code in a decent way... – fabian Apr 19 '22 at 20:34
  • 3
    Side note: `#include` and the general cramped coding style suggests you may be attempting to learn C++ from sites not intended to be learned from like competition sites. You're a lot better off with [a good book or two](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – user4581301 Apr 19 '22 at 20:35
  • Side note: Most development environments thses days come with a debugger. With a debugger you can step through your program line by line and see exactly what the program is doing as it does it. You'll find that taking advantage of the debugger will eliminate the need for the vast majority of Stack Overflow questions AND allow you to write better-targeted, more information-rich questions when Stack Overflow is required. – user4581301 Apr 19 '22 at 20:37
  • 1
    Setting `k` to the string size _before_ the loop yields 0 for length? It should be in the loop. And, after your first `for` loop, `x` will [only] have the value of the _last_ line. You want your last two loops to be _under_ the first loop. – Craig Estey Apr 19 '22 at 20:39
  • 1
    If you can't use a debugger, sit down with [your Rubber Duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging) and explain to it what each line of code does and how it helps you find the solution to your problem. You may find that you are counting the wrong thing completely. – user4581301 Apr 19 '22 at 20:39
  • 1
    At any rate, I strongly suggest printing out the value of `k` – user4581301 Apr 19 '22 at 20:41
  • The first loop reads and discards all but the last string. So the rest of the code won't ever see the first three strings. – Pete Becker Apr 19 '22 at 20:57

1 Answers1

2

The first loop discards all but the last line.

You want the last two loops to be under the first.

Your match logic is reversed. You want to set p to 1 before the inner loop and set it to 0 if the chars do not match.

Your for loop for i must stop short of k. It should be k - l to allow it to not go beyond the end of the x string.

You have the wrong index when scanning x. It should be i + j

And, you want to stop reading lines on a match.


Here's some refactored code:

#include <string>
#include <iostream>
using namespace std;

int
main()
{

    int n,
     p = 0;
    string x,
     y = "yes";
    int l = y.size();

    // get number of input lines
    cin >> n;

    // read in all possible input lines
    for (int i = 0; i < n; i++) {
        // get next input line
        cin >> x;

        // show it
        cout << "Input: " << x << endl;

        // get the length of the string/line
        int k = x.size();

        // loop through all possible starting points for the substring in the
        // line buffer
        // NOTE: we must stop early based on the length of the string we're
        // searching for (hence, the "k - l")
        for (int i = 0; i < (k - l); i++) {
            // say we [might] have a match
            p = 1;

            // loop through chars and stop early if we any corresponding char
            // that does _not_ match
            for (int j = 0; j < l; j++) {
                if (x[i + j] != y[j]) {
                    p = 0;
                    break;
                }
            }

            // print a match and stop
            if (p) {
                cout << "Match: " << x << endl;
                break;
            }
        }

        // stop reading when the line matches
        if (p)
            break;
    }
}

Here is the program output:

Input: noway
Input: ofcourseyousilly
Input: thisisthegayestquestion
Match: thisisthegayestquestion
Craig Estey
  • 30,627
  • 4
  • 24
  • 48