Hey everyone!

I have a problem with my lab if anyone could help check my code that would be great.
I'm supposed to write a program that asks a user for two strings and checks if the beginning of the second string appears anywhere in the first.
It's also supposed to say if that does not occur as well as the position in the first string, where the first character is position 1, and how much of the second string is found in the first string.

This is what I have so far. It does not give the write output for a couple of test cases.

/**File: overlap.cpp
     Name: 
     Synopsis: This program checks if a string is a substring of another. The \
      strings will be given by the user.**/
     #include <iostream>
     #include <cstdlib>
     #include <fstream>
     #include <string>
     using namespace std;
     int main()
    {
        string s1;   //initializing first and second strings
        string s2;
        cout<<"Enter first string"<<endl;  //getting inputs
        getline(cin,s1);
        cout<<"Enter second string"<<endl;
        getline(cin, s2);
        int i=s1.length(); //size
        int j=s2.length(); 
        int position=0;
        int characters=0;
        bool found;
        for(int k=0; k<s1.length(); k++)
        {
            for(int l=0; l<s2.length(); l++)
            {
                if(s2[k]==s1[l])
                {
                    characters=s2.length()-k;
                    position=k+1; 
                    found=true;
                }
                else

              {
                    found=false;
                }
        }
  
        }
            if(true)
            {
                    cout<<"second string overlaps with first string";
                   cout<<" starting at first string position "<<position
    <<" for "<<characters<<" characters of second string."<<endl;
        }
            else
            {
                cout<<"second string does not overlap with first"<<endl;
            }
        return 0;
    }

My test cases are:
=== Test empty strings
--- Input
--- Output
Enter first string:
Enter second string:
Second string does not overlap first string.

=== Test single character strings (overlapping)
--- Input
a
a
--- Output
Enter first string:
Enter second string:
Second string overlaps first string,
starting at first string position 1,
for 1 characters of second string.

=== Test single character strings (non-overlapping)
--- Input
a
b
--- Output
Enter first string:
Enter second string:
Second string does not overlap first string.

=== Test overlapping 1
--- Input
abc
bcd
--- Output
Enter first string:
Enter second string:
Second string overlaps first string,
starting at first string position 2,
for 2 characters of second string.

=== Test overlapping 2
--- Input
abcdef
fedcba
--- Output
Enter first string:
Enter second string:
Second string overlaps first string,
starting at first string position 6,
for 1 characters of second string.

=== Test overlapping 3
--- Input
An entire sentence.
entire
--- Output
Enter first string:
Enter second string:
Second string overlaps first string,
starting at first string position 4,
for 6 characters of second string.

=== Test overlapping 4
--- Input
An entire sentence.
entirety
--- Output
Enter first string:
Enter second string:
Second string overlaps first string,
starting at first string position 4,
for 6 characters of second string.

=== Test non-overlapping 1
--- Input
def
abcdefghi
--- Output
Enter first string:
Enter second string:
Second string does not overlap first string.

=== Test non-overlapping 2
--- Input
abc
def
--- Output
Enter first string:
Enter second string:
Second string does not overlap first string.

=== Test non-overlapping 3
--- Input

abc
--- Output
Enter first string:
Enter second string:
Second string does not overlap first string.

=== Test non-overlapping 4
--- Input
An entire sentence.
bull!
--- Output
Enter first string:
Enter second string:
Second string does not overlap first string.

It looks to me as if you find the first match, but then don't look for the overlap correctly. You should actually have a state machine like this (you don't have to call it that, of course)

state 0: looking for the first place it can match : string1[k] == string2[0]
state 1: found a match in state 0: save index k. Look for overlap
state 2: found a mismatch in state 1: print data and stop
state 3: end of target in state 1: print data and stop
state 4: end of key in state 1: print data and stop
state 5: end of target in state 0: print 'no match' and stop

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.