Im stuck with a problem I need to do for one of my classes...After doing a count I have to declare a char pointer array of the token count size dynamically. In the function findTokens() I have to search through the string, put the address of each token into the pointer array, and change the colon separator to the null character. This is what I got but it doesn't want to work...Any help out there?
Code://... int main(void) { //local declerations char theString[] = "abcd0001:groupno:userno:name:/bin/ksh"; char colon=':'; int count; count = countToken(theString,colon); cout << " Number of tokens in string are: " << count; char **ptr; ptr = new char*[count]; blah = findTokens(theString, ptr,colon); for (int j=0; j < 6; j++) { cout << " Token "<<j<<": " <<blah <<endl<<endl; } }//main int countToken(char theString[], char colon) { int count = 1; // Initialise at one int len = strlen(theString); for(int n = 0; n < len; n++) // Loop from character zero to end of string { if (theString[n] == colon) // Check characters one by one { count++; } } return count; } char findTokens(char* theString, char **ptr, char colon) { int index=0; **ptr[index] = *theString; index++; while(*theString) { if(*theString == colon) { **ptr[index] = (*theString + 1); *theString = '\0'; } *theString++; } return; }



LinkBack URL
About LinkBacks


