Question: C Language Program Modification The following program recognizes numbers, words, and individual punctuation marks. It also accepts free-format input and outputs a single character for
C Language Program Modification
The following program recognizes numbers, words, and individual punctuation marks.
It also accepts free-format input and outputs a single character for each token's type.
For words output, it generates a 'w' and the word, For numbers output 'n' and the number, For punctuation output itself and the EOF should return 'e'.
It uses a global char array lextext[513] to hold the token text.
Sample input:
Voltage = 10 + Current * Resistence; While (n > 5) Do This = That;
Output:
w Voltage = n 10 + W Current * ; w While ( w n > w Do w This = w That ; e
---------------------------------------------------------------------------------------------
#include
char lextext[513] = "Voltage = 10 + Current * Resistence;While (n > 5) Do This = That;"; char *pch; int main() { int num, nIndex = 0, k = 0; pch = strtok (lextext," +-*= /;>()"); while ( pch != NULL) { if(pch[k] >= '0' && pch[0] <= '9') { printf("n %s", pch); } k = 0; memset(str,0, 100); if((pch[k] >= 'A') && (pch[k] <= 'Z') || (pch[k] >= 'a') && (pch[k] <= 'z')){ printf("n %s", pch); }
else printf("%s ", pch); if(pch == NULL) { printf("e"); break; } pch = strtok (NULL, " +-*=/ ;>()"); } return 0; }
---------------------------------------------------------------------------------------------
TASK:
YOUR TASK IS TO MODIFY THE PROGRAM SO THAT IT ASKS THE USER TO:
ENTER AN INPUT (currently the string is fixed) Comment each line of the code & indicate the purpose of variables like pch, strtok, memset
Remove Errors in Line 19 ----------> memset(str,0, 100);
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
