Hi!
I am writing my own string splitter and i'm stuck.
When I'm trying to split the same string the second time im getting only the first word. Im using Visual Studio 2013.
"Name|Phone Number|Account Number|Price|Tariff"
Output is :
Name
Phone Number
Account Number
Price
Tariff
Name
and my desired output is:
Name
Phone Number
Account Number
Price
Tariff
Name
Phone Number
Account Number
Price
Tariff
My Code:
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <ostream>
#include <map>
#include <vector>
#include <string>
using namespace std;
int StrTok(vector<string> &vTokens, char* string, char *delim);
int main()
{
char *p_str;
char str[255] = "Name|Phone Number|Account Number|Price|Tariff|6079777";
char delim[10] = "|";
vector<string> vStr_2;
for (int j = 0; j < 2; j++)
{
StrTok(vStr_2, str, delim);
}
for (int i = 0; i < vStr_2.size(); i++){
cout << vStr_2[i] << "\n";
}
return 0;
}
int StrTok(vector<string> &vTokens, char* string, char *delim)
{
static char *ptr;
const char *p_delim;
char *token;
int i = 0;
if (*string != '\0'){
ptr = string;
token = string;
}
else
{
return vTokens.size();
}
for (; ptr != NULL; ptr = NULL, i++){
while (*ptr != '\0')
{
p_delim = delim;
while (*p_delim != '\0')
{
if (*ptr == *p_delim)
{
*ptr = '\0';
ptr++;
vTokens.push_back(token);
token = ptr;
}
else{
p_delim++;
}
}
ptr++;
}
vTokens.push_back(token);
}
return vTokens.size();
}
Im asking for help to get my desired output.
Thanks in advanced.