Hey everyone! I have a problem with my lab if anyone could help check my codetha
ID: 3619002 • Letter: H
Question
Hey everyone!I have a problem with my lab if anyone could help check my codethat would be great.
I'm supposed to write a program that asks a user for two stringsand checks if the beginning of the second string appears anywherein the first.
It's also supposed to say if that does not occur as well as theposition in the first string, where the first character is position1, and how much of the second string is found in the firststring.
This is what I have so far. It does not give the right output for acouple of test cases.
/**File: overlap.cpp
Name:
Synopsis: This program checks if a string is a substring ofanother. 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 secondstring."<<endl;
}
else
{
cout<<"second string does not overlap withfirst"<<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.