Question

Using ONLY the following header functions: #include <iostream> #include <string> #include <cassert> #include <vector> using namespace...

Using ONLY the following header functions:

#include <iostream>
#include <string>
#include <cassert>
#include <vector>
using namespace std;

Create a C++ function that satisfies the condition using recursion, NO WHILE OR FOR LOOPS.

Pre-condition:
    s.size() == t.size()
Post-condition:
    Returns a vector where the first string is the first character of s followed by the first character of t, the second string is the second character of s followed by the second character of t, and so on.
    For example, zip("abc", "xyz") returns {"ax", "by", "cz"}.
These are the function names:
vector zip(const string& s, const string& t);
void zip_test();

The test function should automatically run when called from main as such:

int main() {

void zip_test();

return 0;

}

!!! No FOR or WHILE Loops Allowed, Not Even the Testing Function !!! Should be done using recursion

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// do comment if any problem arises

// code]Using ONLY the following header functions:

#include <iostream>

#include <string>

#include <cassert>

#include <vector>

using namespace std;

// Pre - condition : s.size() == t.size()

// Post - condition : Returns a vector where the first string is the

// first character of s followed by the first character of t,

// the second string is the second character of s followed

// by the second character of t, and so on.

vector<string> zip(const string &s, const string &t)

{

    static int i = 0;

    if (i == s.size())

    {

        i = 0;

        return (vector<string>){};

    }

    // create s[i]+t[i]

    string temp = "";

    temp = s[i];

    temp += t[i];

    i++;

    // recursive call

    vector<string> temp1 = zip(s, t);

    // add temp to vector

    vector<string> temp2;

    temp2.push_back(temp);

    // add temp1 to temp2 vector

    temp2.insert(temp2.end(), temp1.begin(), temp1.end());

    // return temp2

    return temp2;

}

// For example, zip("abc", "xyz")

// returns{"ax", "by", "cz"}.

void zip_test()

{

    // call zip for testing

    vector<string> testing = zip("abc", "xyz");

    vector<string> output = {"ax",

                             "by",

                             "cz"};

    // testing output

    if (testing == output)

        cout << "Correct\n";

    else

        cout << "Incorrect";

}

int main()

{

    zip_test();

    return 0;

}

Output:

Add a comment
Know the answer?
Add Answer to:
Using ONLY the following header functions: #include <iostream> #include <string> #include <cassert> #include <vector> using namespace...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT