I have a string and I need to split the string into tokens
The word can contain and letter/number but can also contain punctation such as brackets
I need to be able to find an occurence of punctuation, copy the string up to that point as a token, copy the puctuation mark as a token etc until it reaches the end of the string
Here is my output for the string dressed(with)
vector 0: dressed
vector 1: (with)
vector 2: dressed(with
vector 3: )
vectors 0 and 3 are correct but the output should be
vector 0: dressed
vector 1: (
vector 2: with
vector 3: )
here is my code
#include <string>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector <string> vec;
string str = "dressed(with)";
string tmp;
char punct[] = {'+','-','*','/','<','=','!','>','{','(',')','}',';',','};
for (int i=0; i < sizeof(punct); i++)
{
unsigned int pos = str.find(punct[i], 0);
if(pos != string::npos)
{
tmp.assign(str, 0, pos);
vec.push_back(tmp);
tmp.assign(str, pos, pos);
vec.push_back(tmp);
}
}
for(int a=0; a < vec.size(); a++)
{
cout << "vector " << a << ": " << vec.at(a) << endl;
}
return 0;
}