Ask Question
1 July, 01: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:

+5
Answers (1)
  1. 1 July, 03:58
    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?
Get 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 & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers