1. Create a copy of the string (because it needs to be writeable memory).
2. Use strtok_s() or wcstok_s() (latter if you are using wide characters, and you should be) to tokenize.
I haven't tokenized in C++, but I think I once saw someone using the extraction operator from a string stream. I think you can tell the string stream which delimeters you want to use and then simply extract strings, which will be the tokens. Look it up.
So you need to be able to recognize the tokens in the string first before you can start thinking about how to split the string into tokens.
Start with something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
enum token_type { IDENTIFIER /* eg. 'abcd' */, CONSTANT /* eg. '12345' */,
OPERATOR /* eg '+' */, TERMINATOR /* eg. ';' */
/* ... etc ... */, INVALID = -1 };
// what is the type of the token at the start of string str?
// invariant: str does not contain leading white spaces
// and how long (how many characters) is the token?
// note: the match is a 'greedy' match - match as many characters as possible
// eg: str contains 'abc12:= 78 ;'
// result: token_type is IDENTIFIER, token_length is 5
//
// eg: str contains '486)+'
// result: token_type is CONSTANT, token_length is 3
token_type recognize( const std::string& str, /* out */ std::size_t& token_length ) ;
Make sure it is working correctly, and then we can move on to extracting the token.
int main()
{
string str="", line="";
cout << "Enter name of file: ";
cin >> str;
string temp, str1; // Enter the file name
indata.open( str.data() ); // Open file
cout << endl;
int token_length=0;
while (!indata.eof() )
{
indata >> line;
if (! compare(line))
{
for (int r=0; r <line.size(); r++)
{
temp =line.substr(r,1);