This is a function to convert a long string (sentence) into a vector of strings. From the error, it seems obvious that the char ptr newString is causing some access violation, run-time issues. Probably it's length is the root cause? Any help on how to debug/ fix this issue will be appreciated! Also, please note that the output of the function is just coming as expected, but with the run-time error occurring.
vector<string> convertStrgToStrVect(char *inString)
{
unsigned int end = 0, start = 0, i = 0, len = 0, j = 0;
char *inStr = inString, *s;
char newString[] = "";
vector<unsigned int> startLenVect, endLenVect, strLenVect;
vector<unsigned int> :: iterator itr1, itr2;
vector<string> stringVect;
vector<string> :: iterator itr3;
s = inStr;
// Add an extra space an the end of the string
for( i = 0; i < strlen(s); i++)
{
}
s[i] = ' ';
i++;
s[i] = '\0';
cout << s << endl;
cout << strlen(s) << endl;
// Create vectors for start and end indexes to split the words separated by a space
for( i = 0; i < strlen(s); i++)
{
if(s[i] != ' ')
{
end++;
len = end - start;
}
else
{
endLenVect.push_back(end);
startLenVect.push_back(start);
strLenVect.push_back(len);
end = i+1;
start = i+1;
}
}
cout << "startLenVect: ";
for(itr1 = startLenVect.begin(); itr1 != startLenVect.end(); itr1++)
{
cout << *itr1 << " ";
}
cout << "endLenVect: ";
for(itr1 = endLenVect.begin(); itr1 != endLenVect.end(); itr1++)
{
cout << *itr1 << " " << endl;
}
for(itr1 = startLenVect.begin(), itr2 = endLenVect.begin(); itr1 != startLenVect.end(); itr1++, itr2++)
{
strcpy(newString, "");
for(i = *itr1, j = 0; i < *itr2; i++, j++)
{
newString[j] = s[i];
}
newString[j] = '\0';
stringVect.push_back(newString);
}
for(itr3 = stringVect.begin(); itr3 != stringVect.end(); itr3++)
{
cout << "stringVect: " << *itr3 << endl;
}
return stringVect;
}
Aucun commentaire:
Enregistrer un commentaire