Question

In: Computer Science

c++ ///////////////////////////////////////////////////////////////////////// need to be named Subsequences.h and pass test cpp file SubsequenceTester.cpp at the end...

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.

  • Pin is a subsequence of Programming
  • ace is a subsequence of abcde
  • bad is NOT a subsequence abcde as the letters are not in order

This assignment you will create a Subsequence class to do the following:

  • Add a constructor that takes only a single word argument
  • Add a bool isSubsequence(string sub); a method that will return true if sub is a subsequence of the original word.
  • Overload the operator<< using a friend function, such that information is given about the class. This will be used for your own debugging

Hints

Think about the following questions to help you set up the recursion...

  • What strings are subsequences of the empty string
  • What happens if the first character of the subsequence matches the first character of the text?
  • What happens if it doesn't

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;
    }
}

Solutions

Expert Solution

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.


Related Solutions

Using C++ Create the UML, the header, the cpp, and the test file for an ArtWork...
Using C++ Create the UML, the header, the cpp, and the test file for an ArtWork class. The features of an ArtWork are: Artist name (e.g. Vincent vanGogh or Stan Getz) Medium (e.g. oil painting or music composition) Name of piece (e.g. Starry Night or Late night blues) Year (e.g. 1837 or 1958)
****************************************Instructions**************************************************** I just need a test .cpp client file that tests the new bold bits of...
****************************************Instructions**************************************************** I just need a test .cpp client file that tests the new bold bits of code of the courseListType class. It can be relatively simple. :) Just want to let you know this is Part 2 of the question previously answered at: https://www.chegg.com/homework-help/questions-and-answers/right-courselisttype-hold-static-50-courses-need-make-little-flexible-want-change-static-a-q57690521 *********************************************************************************************************** //courseTypeList.h #ifndef COURSELISTTYPE_H_INCLUDED #define COURSELISTTYPE_H_INCLUDED #include #include "courseType.h" class courseTypeList { public: void print() const; void addCourse(std::string); courseTypeList(int n); ~courseTypeList(); private: int NUM_COURSES; int courseCount; courseType *courses; }; #endif // COURSELISTTYPE_H_INCLUDED ==================================================== //courseTypeList.cpp #include #include...
answer in c++ First – take your Box02.cpp file and rename the file Box03.cpp Did you...
answer in c++ First – take your Box02.cpp file and rename the file Box03.cpp Did you ever wonder how an object gets instantiated (created)? What really happens when you coded Box b1; or Date d1; or Coord c1; I know you have lost sleep over this. So here is the answer………. When an object is created, a constructor is run that creates the data items, gets a copy of the methods, and may or may not initialize the data items....
This class should include .cpp file, .h file and driver.cpp (using the language c++)! Overview of...
This class should include .cpp file, .h file and driver.cpp (using the language c++)! Overview of complex Class The complex class presents the complex number X+Yi, where X and Y are real numbers and i^2 is -1. Typically, X is called a real part and Y is an imaginary part of the complex number. For instance, complex(4.0, 3.0) means 4.0+3.0i. The complex class you will design should have the following features. Constructor Only one constructor with default value for Real...
ceate a Visual Studio console project named exercise093. In the exercise093.cpp file first define a function...
ceate a Visual Studio console project named exercise093. In the exercise093.cpp file first define a function void lowerToUpper(std::string & sentence) that iterates over all characters in the sentence argument. Any lowercase letter should be converted to uppercase. This can be done by including and testing each character in sentence with the islower() function. If islower(sentence[i]) returns true then sentence[i] should be replaced with toupper(sentence[i]). The main() function should assign "Hello how are you doing?" to sentence, call lowerToUpper(sentence), and use...
Create a Visual Studio console project named exercise093. In the exercise093.cpp file first define a function...
Create a Visual Studio console project named exercise093. In the exercise093.cpp file first define a function void lowerToUpper(std::string & sentence) that iterates over all characters in the sentence argument. Any lowercase letter should be converted to uppercase. This can be done by including <cctype> and testing each character in sentence with the islower() function. If islower(sentence[i]) returns true then sentence[i] should be replaced with toupper(sentence[i]). The main() function should assign "Hello how are you doing?" to sentence, call lowerToUpper(sentence), and...
c++ Write the implementation (.cpp file) of the Counter class of the previous exercise. The full...
c++ Write the implementation (.cpp file) of the Counter class of the previous exercise. The full specification of the class is: A data member counter of type int. An data member named counterID of type int. A static int data member named nCounters which is initialized to 0. A constructor that takes an int argument and assigns its value to counter. It also adds one to the static variable nCounters and assigns the (new) value of nCounters to counterID. A...
Using C++ code, write a program named q5.cpp to print the minimum of the sums x...
Using C++ code, write a program named q5.cpp to print the minimum of the sums x + y^3 and x^3 + y, where x and y are input by a user via the keyboard.
Write a C++ program based on the cpp file below ++++++++++++++++++++++++++++++++++++ #include using namespace std; //...
Write a C++ program based on the cpp file below ++++++++++++++++++++++++++++++++++++ #include using namespace std; // PLEASE PUT YOUR FUNCTIONS BELOW THIS LINE // END OF FUNCTIONS void printArray(int array[], int count) {    cout << endl << "--------------------" << endl;    for(int i=1; i<=count; i++)    {        if(i % 3 == 0)        cout << " " << array[i-1] << endl;        else        cout << " " << array[i-1];    }    cout...
C++ Download Lab10.cpp . In this file, the definition of the class personType has given. Think...
C++ Download Lab10.cpp . In this file, the definition of the class personType has given. Think of the personType as the base class. Lab10.cpp is provided below: #include <iostream> #include <string> using namespace std; // Base class personType class personType { public: void print()const; //Function to output the first name and last name //in the form firstName lastName. void setName(string first, string last); string getFirstName()const; string getLastName()const; personType(string first = "", string last = ""); //Constructor //Sets firstName and lastName...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT