// A simple example of using a vector to store a list of strings
// Works by takeing a input stream then breaking up when a char such as a space of , is found.
// By Ben a.k.a DreamVB 
// 00:00 29/09/2016

#include <iostream>
#include <vector>

using namespace std;
using std::cout;
using std::endl;
void Split(char* src, char del, vector<string>&List){
	//vector<string>List;
	int pos = 0;
	int i = 0;
	char MyWord[128];
	List.clear();
	//Split a string
	while(i < strlen(src)){
		if(src[i] == del){
			//Append ending char
			MyWord[pos] = '\0';
			//Check length
			if(strlen(MyWord) != 0){
				//Push iten into vector
				List.push_back(MyWord);
			}
			pos = 0;
		}
		//If no del is found add chars
		if(src[i] != del){
			//Deal with quotes in the string
			if(src[i] == '"'){
				i+=1;
				//Loop till we find next quote
				while(src[i] != '"'){
					//Build the word
					MyWord[pos] = src[i];
					//INC counters
					pos++;
					i++;
				}
				MyWord[pos] = '\0';
			}else{
				MyWord[pos] = src[i];
			}
			pos++;
		}
		//Keep src counter going.
		i++;
	}
	//Append remaining
	MyWord[pos] = '\0';
	if(strlen(MyWord)){
		List.push_back(MyWord);
	}
	//Clear up
	memset(&MyWord[0],0,sizeof(MyWord));
}
void DisplayVector(vector<string>vec){
	int i = 0;
	
	while(i < vec.size()){
		//Print out items
		cout << (i+1) << " " << vec[i].c_str() << endl;
		i++;
	}
	cout << endl;
}
int main(int argc, char **anvg, char**envp){
	char *Sample = new char[255];
	strcpy(Sample,"Wlecome to \"C++ VB JAVA DELPHI VBA\" Power \"Programming is fun\"");
	//Display contents of vector.
	vector<string>List;
	cout << "Source: " << Sample << endl << endl;
	//Sample one with space
	Split(Sample,' ',List);
	//Show vector contents
	DisplayVector(List);
	strcpy(Sample,"\"Mr Jack Jones\",$2505,18,\"Job Programmer, Designer\"");
	cout << "Source: " << Sample << endl << endl;
	
	Split(Sample,',',List);
	//Show vector contents
	DisplayVector(List);
	delete[]Sample;
	system("pause");
	return 1;
}