In: Computer Science
c++
/////////////////////////////////////////////////////////////////////////
need to be named Subsequences.h and pass test cpp file SubsequenceTester.cpp at the end of this question.
This assignment will help you solve a problem using recursion
Description
A subsequence is when all the letters of a word appear in the relative order of another word.
This assignment you will create a Subsequence class to do the following:
Hints
Think about the following questions to help you set up the recursion...
Submission
To get you thinking of data good test cases, only the example test cases have been provided for you in the SubsequenceTester.cpp. It is your responsibility to come up with several other test cases. Think of good ones
////////////////////////////////////////////////////
SubsequenceTester.cpp
#include <iostream>
#include "Subsequences.h"
using namespace std;
void checkCase(string, string, string, bool);
int main()
{
/**
Add several more test
cases to thoroughly test your data
checkCase takes the
following parameters (name, word, possible subsequence, true/false
it would be a subsequence)
**/
checkCase("Case 1: First Letter", "pin",
"programming", true);
checkCase("Case 2: Skipping Letters", "ace",
"abcde", true);
checkCase("Case 3: Out of order", "bad",
"abcde", false);
return 0;
}
void checkCase(string testCaseName, string sub, string sentence,
bool correctResponse){
Subsequences s(sentence);
if(s.isSubsequence(sub) ==
correctResponse){
cout << "Passed "
<< testCaseName << endl;
}
else{
cout << "Failed "
<< testCaseName << ": " << sub << " is "
<< (correctResponse? "": "not ") << "a subsequence of "
<< sentence << endl;
}
}
ANSWER:
C++ Code:
#include <iostream>
using namespace std;
int checkCase(string, string);
int main() {
string subsq, str;
int flag;
cout << "Enter substring : ";
getline(cin, subsq);
cout << "Enter string : ";
getline(cin, str);
flag = checkCase(subsq, str);
if(flag)
cout << subsq << " is a susequence of " << str;
else
cout << subsq << " is not a susequence of " << str;
return 0;
}
int checkCase(string sq, string st)
{
int subindex = 0, strindex = 0;
while(sq[subindex] != '\0')
{
while( (sq[subindex] != st[strindex]) && (st[strindex] != '\0'))
{
strindex++;
}
if(st[strindex] == '\0')
break;
subindex++;
strindex++;
}
if(sq[subindex] == '\0')
return 1;
else
return 0;
}
Please refer to the screenshots of the code to understand the indentation of the code.
If you do not get anything in this solution,please put a comment and i will help you out.
Do not give a downvote instantly.It is a humble request.
If you like my answer,please give an upvote ....Thank you.