Originally Posted by
paranoidgnu
Nothing changed here,
You missed a couple of crucial bits, and you completely missed the point:
Code:
do{
scanf("%c", &transition_char[i]);
i++;
current_state = transition(current_state, transition_char[i-1]);
if (current_state == identifier)
tag = identifier;
else if(current_state == number)
tag = number;
printf("%c",transition_char[i-1]);
}while(transition_char[i-1] != ' ' && transition_char[i-1] != '.');
That just reproduces your original mistake.
The idea is simple: an "identifier" has letters, and possibly numbers. A "number" only has numbers. So if during that do..while, current_state comes back as "identifier", the tag state for that tag/word/item is identifier. Period. End of story. So (and you missed this bit, but it is in the code from my previous post) you set the tag state to "number" before the do..while:
Now, you go thru the loop and if you find alphabetic characters, set tag to "identifier":
Code:
do{
scanf("%c", &transition_char[i]);
i++;
current_state = transition(current_state, transition_char[i-1]);
if (current_state == identifier) tag = identifier;
printf("%c",transition_char[i-1]);
} while (transition_char[i-1] != ' ' && transition_char[i-1] != '.');
Notice, I DID NOT EXPLICITLY SET TAG TO "NUMBER" INSIDE THIS LOOP. Tag is already "number"; if it changes to "identifier", IT SHOULD REMAIN THAT WAY AND NOT BE CHANGED BACK TO "NUMBER".
The reason I haven't posted the whole program is because: 1) actually it is all here, but in seperate bits, 2) I think it is important for you to think about the changes and the logic behind them. But if you are desperate, ask And it does work, here's the output:
Code:
[root~/C] echo "rate R2D2 48 2 time 555666 ." | ./a.out
Enter text> rate - Identifier
R2D2 - Identifier
48 - Number
2 - Number
time - Identifier
555666 - Number