Ask Question
16 May, 10:44

Given the following header: vector split (string target, string delimiter); implement the function split so that it returns a vector of the strings in target that are separated by the string delimiter. For example: split ("10,20,30", ",") should return a vector with the strings "10", "20", and "30". Similarly, split ("do re mi fa so la ti do", " ") should return a vector with the strings "do", "re","mi", "fa", "so", "la", "ti", and "do". Write a program that inputs two strings and calls your function to split the first target string by the second delimiter string and prints the resulting vector all on line line with elements separated by commas. A successful program should be as below with variable inputs:

+2
Answers (1)
  1. 16 May, 13:09
    0
    see explaination

    Explanation:

    #include

    #include

    #include

    using namespace std;

    vector split (string, string);

    int main ()

    {

    vector splitedStr;

    string data;

    string delimiter;

    cout << "Enter string to split:" << endl;

    getline (cin, data);

    cout << "Enter delimiter string:" << endl;

    getline (cin, delimiter);

    splitedStr = split (data, delimiter);

    cout << "/n";

    cout << "The substrings are: ";

    for (int i = 0; i < splitedStr. size (); i++)

    cout << "/"" << splitedStr[i] << "/"" << ",";

    cout << endl << endl;

    cin >> data;

    return 0;

    }

    vector split (string target, string delimiter)

    {

    unsigned first = 0;

    unsigned last;

    vector subStr;

    while ((last = target. find (delimiter, first)) ! = string::npos)

    {

    subStr. push_back (target. substr (first, last-first));

    first = last + delimiter. length ();

    }

    subStr. push_back (target. substr (first));

    return subStr;

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Given the following header: vector split (string target, string delimiter); implement the function split so that it returns a vector of the ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers