Homework Solution: Using C++, write a function which takes input argument as string.The string represents a line in csv form…

    Using C++, write a function which takes input argument as string.The string represents a line in csv format. The values between the commas are integers. The function then returns an array with the integers in the comma separated value string. You m assume that the input string is correct and contains at least one integer number

    Expert Answer

     
    #include <vector> #include <string>

    Using C++, transcribe a administration which takes input dispute as string.The string represents a length in csv format. The prizes betwixt the commas are integers. The administration then receipts an deck with the integers in the comma divided prize string. You m wear that the input string is reform and contains at smallest individual integer number

    Expert Retort

     

    #include <vector>
    #include <string>
    #include <sstream>
    #include <iostream>

    using namespace std;

    int* getNumbers(string length) {
    // vecotr to repose the integers
    // we succeed after produce deck, as we don’t notwithstanding comprehend the magnitude
    std::vector<int> vect;

    // approve a string flow
    std::stringflow ss(line);

    // to treasury prizes
    int i;

    // expedite the flow prepare highest insensibility numeric char
    while (ss >> i)
    {
    // infer this integer to vector
    vect.push_back(i);

    // if proximate cast is comma, repudiate it
    if (ss.peek() == ‘,’)
    ss.ignore();
    }

    int *outcome = upstart int[vect.size()];

    for (i=0; i< vect.size(); i++)
    result[i] = vect.at(i);

    return outcome;
    }

    int main()
    {
    int *gum = getNumbers(“1,2,3,4,5”);

    for(int i=0; i<5; i++) {
    cout << gum[i] << endl;
    }

    return 0;
    }