How does delimiter work>

Hey so I am little confused on how delimiter work. I need write a function that splits the string by delimiter. Function header is:
vector<string> split(stirng target, string delimiter);
for example, the code inside main calling your function
vector<string> v = split("hiAAAmyAAAnameAAAis", "AAA");
and v should come out as hi my name is

So is it something like
vector<string> split(string target, string delimiter)
{
vector<string> word;
string s = "hiAAAmyAAAnameAAAis";
string delimiter = "AAA";
Use string::find and string::substr
See http://www.cplusplus.com/reference/string/string/substr/

If you find a delimiter, then next search should start after it. Repeat until at end of target.
could you give me like a pseudocode? Thank you..
okay i got this far but its not giving me the answer
#include <string>
#include <vector>
#include <iostream>

using namespace std;
vector<string> split (string target, string delimiter)
{
vector<string> thing;
thing.push_back(delimiter);
return thing;
}


int main () {
vector<string> v = split("hiAAAmyAAAnameAAAis", "AAA");
cout << v.at(0);
system ("pause");
return 0;
}
Last edited on
Please, use code tags. Your code:
1
2
3
4
5
6
vector<string> split( string target, string delimiter )
{
  vector<string> thing;
  thing.push_back( delimiter );
  return thing;
}

What are doing on line 4?
i am trying to delete off AAA
I dont know how to use the code tage
On the right side of the tab format: hover over this symbol (<>). And put it in between code and /code.
 
//like this 
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <string>
#include <vector>
#include <iostream>

using namespace std;
    vector<string> split (string target, string delimiter) 
	{
	vector<string> v;
	v.push_back(delimiter);
	return v;
}


int main () {
	vector<string> v = split("myAAAnameAAAis", "AAA");
	cout << v.at(0)<<endl;
	system ("pause");
	return 0;
}
Last edited on
What you try is one thing. I did ask what your code does.
What does the push_back do?
What do you push back in that statement?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <string>
#include <vector>
#include <iostream>

using namespace std;

vector<string> split_str(const string& str, const string& delimeter){
  vector<string> result;
  string::const_iterator begin, str_it; 
  // Point to the first character in the input string.
  str_it = str.begin(); 
  // Get to the first non-delimit character in string.
  while (delimeter.find(*str_it) != string::npos && str_it != str.end())
    str_it++;
  // Make "begin" point to the first non-delimit character in string.
  begin =  str_it;    
  do {
    // Find position of first delimit character in str
    while (delimeter.find(*str_it) == string::npos && str_it != str.end())
      str_it++;
    string token = string(begin, str_it); // make a copy of the valid token.
    result.push_back(token); // push it into the vector
    // Skip through the additional consecutive delimit characters
    while (delimeter.find(*str_it) != string::npos && str_it != str.end())
      str_it++; 
    // reset the begin to the start of the remaining text.
    begin = str_it;
  } while (str_it != str.end());
  return result;
}

int main()
{
  vector<string> v = split_str("hiAAAmyAAAnameAAAis", "AAA");
  for (vector<string>::const_iterator it = v.begin(); it != v.end(); it++)
    cout << *it << "\n";
  return 0;
}
Topic archived. No new replies allowed.