Question:
C++: Need help defining my own string tokenizer function?
James Bond 007
2012-02-09 13:53:04 UTC
I been having some problems with this. My teacher does not want us to use the strtok() function to tokenize my string. What he wants instead is for us to define our own strtok(). He wants us to use 2 character arrays, 1 to copy the string, & another to tokenize the characters. The array that is used for the tokenizing part will return the word seperated by a white space. However, that's the hard part, & I would greatly appreciate some help.

MY CODE:
================================================
#include
#include
#include
#include
#include
using namespace std;

//prototype functions
bool isWhiteSpace(char); //check for white space
char* getToken(char*); //get the token of the input\

//global variables & arrays
string myString; //global string
int i = 0; //global counter

void main()
{
while(1) //infinite loop
{
//prompt user to enter a command
cout << "Command: ";
getline(cin, myString); //100 max characters

//if user enters "quit" anywhere in the string,
//break out of the loop
if(!myString.find("quit"))
{
//error message
cout << "\t***Terminate program***" << endl;
break; //break out of loop
}
else //tokenize the string
{
char* c = myString.c_str();
string token = getToken(c);
cout << token << endl; //print out the tokenize string
}
}
system("pause"); //pause screen
}

//This function will check if there is any white space
//in the string. It will return "true" if there is.
bool isWhiteSpace(char* myString)
{
if(*myString == ' ' || *myString =='\t' || *myString =='\n' ||
*myString =='\v' || *myString =='\f' || *myString =='\r')
return true; //return true if there is a white space
//else if(myString.eof())
// return false; //return false if end of file
else
return false; //return false if there is no white space
}

//This function will move through the input buffer character by
//character. Each invocation will ignore white spaces. When
//it finds the first non-white space it will copy the non-white
//character to a token buffer. When a white space character is
//found, it will put a NULL terminator at the end of the token
//buffer. The token buffer will then be returned to the calling
//function. If the end of the input is reached, the getToken()
//function will return a NULL string.
char* getToken(string s)
{

//const char* c = s.c_str(); //convert the string to char array
char* token = (char*)malloc(sizeof(s.size()) + 1);
//buffer to convert string to char*
strcpy(token, s.c_str()); //copy s (converted) into token

//token[s.size()+1] = 0;
//memcpy(token,s.c_str(),s.size());
//strtok(token, " ");

//NEED HELP HERE!!!!!
//loop until NULL is found
while(token != NULL)
{
//loop through string
for(int a= 0; a <= s.size(); a++)
{
for(int b = 0; b <= a-1; b++)
{
//check for white spaces
if(!isWhiteSpace(token[a]))
{
//if not true, then put NULL at the end of token
token[a] = token[b]; //copy the elements, add NULL char
return token + '\0'; //add null at the end
//token = strtok(NULL, " "); //copy the contents in src into token
//return token;
}
else
{
token[a] = token[a];
return token;
//return token;
}
}
}


i++; //advance to next character
}
}
Three answers:
JockyMc
2012-02-10 11:49:04 UTC
I'm not exactly sure what you're after. If I pass in the string "Is this working", what would you expect the program to return? The code I have written below will return the 1st token (If). Is it meant to go through each token in the string? i.e. by calling getToken 3 times? Also, if you reach the end of the string, why return NULL? Why not return the token up to that point?



char* getToken(string s)

{

//const char* c = s.c_str(); //convert the string to char array

char* token = (char*)malloc(sizeof(s.size()) + 1);

if (token != NULL)

{

// initialise all to NULL

memset (token, NULL, s.size()+1);



for (int a=0; a
{

// is it a NULL?

if (s[a] == NULL)

{

// yes, so...

// delete what we've allocated so far

delete [] token;

// return a NULL string;

token = NULL;

}

else if (isWhiteSpace(s[a]) == false)

{

token[a] = s[a];

}

// if it's a whitespace, do nothing

// as the array has already been initialised

// to NULL

}

}

else

{

// error allocating memory

}

return token;

}
desporte
2016-11-15 07:59:31 UTC
Tokenizer C
cansler
2016-10-22 08:47:02 UTC
putchr takes a single character as a parameter. putchr('A'); could deliver the letter A. printstr takes a pointer to an array of characters. The * shows that this is a pointer. The const is a promise to the compiler which you will no longer regulate the archives it fairly is exceeded in, in basic terms study it. you do no longer desire const in putchar, using fact a sing char is exceeded by way of fee, no longer by way of reference. whether you alter the fee of c in putchar, this is barely changing the community replica that grow to be exceeded in, no longer the unique character. With regulations, in case you alter the archives the pointer factors to, you're changing what grow to be initially exceeded in. const says...I won't do this. in case you have the my_printf function after significant, and you haven't any longer have been given a prototype for it, then it fairly is probably your blunders. placed the full function, or a prototype for the function in the previous significant. The prototype could appear like this: void my_printf(const char *string); If that's no longer the subject, then placed up the suitable blunders message you're becoming to be. You do understand that my_printf is barely going to print a single character, precise? The letter H for that reason.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...