binary tree token problem
Hi there... I've got a question about tokenizing strings...
I'm attempting to write a program that inputs a sentence from the user, tokenizes it and then hands the words to a binary search tree. I am using 'strtok' to to tokenize the sentence so the original is in a char format. I can print the individual tokens using a pointer and everything works great until I hand the tokens to the tree insert function. It appears that the program only hands off the first character of each word instead of the whole token. I get warnings that the individual first letters are dups (which is a check in the insertNode function template) so I think the function call to insertNode might be the culprit although I can't figure out a way to hand the tokens off any other way (I tried several).
Here is the pertinent code (I omitted the header files because I suspect the problem is in the function call ):
Code:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::fixed;
#include <cstring> // prototype for strtok
#include "tree.h" // Tree class definition
int main()
{
Tree< char > charTree; // create Tree
char sentence[150];
char *tokenPtr;
cout << "Enter a sentence (150 characters max):\n";
cin.getline( sentence, 150, '\n' );
cout << "The string to be tokenized is:\n" << sentence
<< "\n\nThe tokens are:\n\n";
// begin tokenization of sentence
tokenPtr = strtok( sentence, " " );
// continue tokenizing sentence until tokenPtr becomes NULL
while ( tokenPtr != NULL ) {
charTree.insertNode( *tokenPtr );
cout << tokenPtr << '\n';
tokenPtr = strtok( NULL, " " ); // get next token
} // end while
cout << "\nPreorder traversal\n";
charTree.preOrderTraversal();
cout << "\nInorder traversal\n";
charTree.inOrderTraversal();
cout << "\nPostorder traversal\n";
charTree.postOrderTraversal();
cout << endl;
return 0;
} // end main
The assignment suggested to use the strtok function so I assumed that this would be the easiest way to accomplish the tokenization of the sentence. I checked many of the previous binary tree threads here but could not find anything that seemed similar to this particular problem. I'm probably missing something obvious but I can seem to figure out why the whole word is not getting passed to the function...
Any assistance would be appreciated. Thanks...