Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here is what I have currently: int main() { char answer = \'y\'; do{ string s1,

ID: 3914487 • Letter: H

Question

Here is what I have currently:

int main()

{

char answer = 'y';

do{

string s1, s2;

cout << "Input two strings: ";

cin >> s1 >> s2;

int same = 0;

int s2length = s2.length();

int s1length = s1.length();

int length = s2length - 1;

for (int i = 0; i < s1length; i++)

{

for (int j = 0; j < s2length; j++)

{

if (s1.at(i) == s2.at(j))

{

same++;

length-=1;

i++;

cout << s1.at(i) << s2.at(length);

}

}

}

cout << endl << "Longest match: " << s1.substr(0,same) << endl;

cout << "match length: " << same << endl;

cout << "Input 2 more strings [y/n]? ";

cin >> answer;

}

while (answer == 'y');

return 0;

}

Problem A: (A Type of) Sequence Matching (20 points) One string analysis task that played a crucial role in mapping the human genome is the following: given two strings s1 and s2, find the largest substring occurring at both the very beginning of s1 and the very end of s2. For example, if s1 is the string ACTTTCTAT and s2 is the string TGTGTTTTGAAAAAACT, notice that ACT occurs at the beginning of s1 and the end of s2. Moreover, no longer substring occurring at the beginning of s1 also occurs at the end of s2: for example, the first four characters of s1, namely ACTT, are not the final four characters of s2; the first five characters of s1, namely ACTTT, are not the final five characters of s2; and so on. Write a C++program that does the following: 1. Asks the user to input the two strings s1 and s2. 2. Finds the longest substring that occurs both at the beginning of s1 and the end of ??. 3. Prints out that substring and its length. . Has a continuation loop, so the user can repeat the above steps as many times as he or she likes Here are additional comments and requirements Do not make any assumptions about the strings' lengths. In particular, the two strings might have different lengths. Your program should store the strings as C++-style strings You may use any of the string class functions mentioned in the textbook. Your solution should not be a long program, but think carefully about the program

Explanation / Answer



Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
#include <iostream>
#include <string>
using namespace std;
int main()
{
char answer = 'y';
do{
string s1, s2;
cout << "Input string s1: ";
cin >> s1;
cout << "Input string s2: ";
cin >> s2;
int s1length = s1.length();
int s2length = s2.length();
int maxLen = 0;
for(int len = 1; len <= s1length; len++)
{
bool match = true;
if(s2length - len < 0) //not enough characters in s2
break;
//check if len characters match
for (int i = 0, j = s2length - len; i < len; i++, j++)
{
if(s1[i] != s2[j])
{
match = false;
break;
}
}
if(match)
maxLen = len;
}
cout << endl << "Longest match: " << s1.substr(0,maxLen) << endl;
cout << "Match length: " << maxLen << endl;
cout << "Input 2 more strings [y/n]? ";
cin >> answer;
}
while (answer == 'y');
return 0;
}


output
-----
Input string s1: ACTTTCTAT
Input string s2: TGTGTTTTGAAAAAACT
Longest match: ACT
Match length: 3
Input 2 more strings [y/n]? n