Critique my StringTokenizer please...
Hi all, this is a string tokenizer that I wrote in C++ quite a while ago, I wrote it because I hate strtok, I hate strtok because it requires me to make multiple calls to get all the tokens. The point of this function is that it returns an array of strings each containing a token. The code is fully documented, what i'm looking for here is basically a code critique, if anyone can spot any flaws or possible memory leaks I would love to hear about them, hope someone can help me out, thanks all!
Code:
char** StringTokenizer(const char* pString, const char* pDelimiters, int& nTokenCount)
{
char** pSuperString; //the array of strings that holds everything
int nInputLength = StringLength(pString); //get length of string to be tokenized
int nNumDelimeters = StringLength(pDelimiters); //get how many delimiters there are
int nSuperString = 0; //used as a counter
int nString = 0; //used as a counter
char* pMyInput = new char[nInputLength]; //just a copy of pString so as not to modify original string
StringCopy(pMyInput, pString);
for (int nInput = 0; nInput < nInputLength; nInput++) //insert spaces where the tokens are (acts as a placeholder)
{
for (int nToken = 0; nToken < nNumDelimeters; nToken++)
{
if (pMyInput[nInput] == pDelimiters[nToken])
{
pMyInput[nInput] = ' ';
}
}
nToken = 0;
}
for (int nCount = 0; nCount < nInputLength; nCount++) //count number of tokens (spaces)
{
if (pMyInput[nCount] == ' ' && pMyInput[nCount+1] != ' ')
{
nTokenCount++;
}
}
nTokenCount++;
pSuperString = new char*[nTokenCount]; //number of tokens (+1) = number of strings
for (int nSString = 0; nSString < nTokenCount; nSString++)
{
pSuperString[nSString] = new char[50]; //go through an assign memory to each string within the superstring
StringCopy(pSuperString[nSString], "");
}
for (int nCounter = 0; nCounter < nInputLength; nCounter++)
{
if (pMyInput[nCounter] != ' ')
{
StringCopy(pSuperString[nSuperString][nString], pMyInput[nCounter]); //copy substrings into the superstring
nString++;
}
else if (pMyInput[nCounter+1] != ' ')
{
nSuperString++;
nString = 0;
}
}
return pSuperString;
}
StringCopy and StringLength
Here are my StringCopy and StringLength functions which are used in my tokenizer
Code:
void StringCopy(char* pDestination, const char* pSource)
{
int nCounter = 0;
int nNewLength = StringLength(pSource);
while (nCounter < nNewLength)
{
pDestination[nCounter] = pSource[nCounter];
nCounter++;
}
pDestination[nCounter] = '\0';
}
Code:
int StringLength(const char* pString)
{
int nCounter = 0;
while (pString[nCounter] != NULL) { nCounter++; }
return nCounter;
}