Answer to Question #196845 in C++ for Mpopo

Question #196845

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("do,re,me,fa,so,la,ti,do", ",") should return a vector with the strings "do", "re", "me", "fa", "so", "la", "ti" and "do". Test your function split() in a driver program that displays the strings in the vector after the target has been split. 


1
Expert's answer
2021-05-23T14:36:33-0400


#include<iostream>
#include<bits/stdc++.h>
using namespace std;


vector<string> split(string s, char t)
{
	vector<string>v1;
	string str="";
	for(int i=0; i<s.length(); i++)
	{
		if(s[i] == t)
		{
			v1.push_back(str);
			str="";
		}
		else{
			str += s[i];
		}
	}
	
	v1.push_back(str);
	return v1;
}


int main()
{
	cout<<"Enter string : ";
	string s;
	cin>>s;
	char t;
	cout<<"Enter delimiter : ";
	cin>>t;
	vector<string>v;
	
	v = split(s,t);
	for(int i=0; i<v.size(); i++)
	{
		cout<<v[i]<<" ";
	}
}




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog