Please use the C thank you so much Write a function named ne

Please use the C++, thank you so much!

Write a function named next St ring that will return a single \'value\' (i.e. substring) from a Comma Separated Value\" string. Your function will take two arguments: a string variable containing a comma separated list of values, and an integer containing the starting index; your function should return a single string object with the value that starts at that index and ends right before the next comma \', \' (do not include the comma in the returned string!): If, however, the start index is after the last comma in the string, then the function should return the value starting at that index and continuing to the end of the string. When you have written your function, then write a short test program that will take in a simple comma separated string using get line: getline(cin, somestring) and output values in the string using the nextString function.

Solution

Please follow the code and comments for description :

CODE :

#include <iostream> // required header files
#include <string>
#include <iomanip>
#include <cstddef>

using namespace std;

string nextString(string str, int start_index); // function declaration

string nextString(string str, int start_index) { // function initialisations
   string subString = str.substr(start_index); // string that is got after the specified index
   size_t comma = subString.find(\",\"); // getting the string after the first occurrence of the delimited value
   string resStr = subString.substr(0, comma); // saving the data
   return resStr; // returning the result
}

int main() // driver method
{  
   string input; // local initialisations
   getline(cin, input); // function that reads the user input
   string out = nextString(input,18); // calling the method to get the desired output
   cout << out << endl; // print the data to console

   return 0;
}


OUTPUT :

my,cat,ate,my,homework
work


Hope this is helpful.

Please use the C++, thank you so much! Write a function named next St ring that will return a single \'value\' (i.e. substring) from a Comma Separated Value\

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site