Ask Question

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. 2 March, 20:32
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers