Question
Compile and run the program found in readcmd.c. This program accepts single letter commands as input and simply prints out the command it read. Any
Compile and run the program found in readcmd.c. This program accepts single letter commands as input and simply prints out the command it read. Any blanks in front of the command are ignored, as are any characters after the command. You should type in input lines like the following to see how it behaves:
a a and this is a long line
One of the things you will notice about this program is that it is not documented with comments for the algorithm or for the functions. Your first task is to document this code. Copy the program into myreadcmd.c, and extend this file to ignores leading tabs as well as leading blanks (or any combinations of blanks and tabs). Now extend the program to have either a semi-colon or a newline character indicate the end of a command. Here's a sample run:
a ; b The command is: a The command is: b a The command is: a
Now extend the program to verify that the command is an upper or lower case letter. It should print an error message if it isn't. Also, verify that the line has a command: if it hits a blank or ; without an intervening command, you should print an error. Here's a sample run:
a ; b The command is: a The command is: b + Error: + is not a letter. / Error: / is not a letter. Error: missing command. ; b Error: missing command. The command is: b
/* * Read a command, skipping over leading blanks and any trailing * characters. */ #includeint skipBlanks(void); int skipOverRestOfCommand(void); int main() { int cmd; cmd = skipBlanks(); while (cmd != EOF) { printf("The command is: %c ", cmd); skipOverRestOfCommand(); cmd = skipBlanks(); } } int skipBlanks(void) { int c; c = getchar(); while (c == ' ') c = getchar(); return c; } int skipOverRestOfCommand(void) { int c; c = getchar(); while (c != ' ') c = getchar(); return c; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started