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.
#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]<<" ";
}
}
Comments
Leave a comment