Question

In: Computer Science

Write a C program that repeatedly prompts the user for input at a simple prompt (see...

Write a C program that repeatedly prompts the user for input at a simple prompt (see the sample output below for details). Your program should parse the input and provide output that describes the input specified. To best explain, here's some sample output:

ibrahim@ibrahim-latech:~$ ./prog1

$ ls -a -l -h

Line read: ls -a -l -h

Token(s):

ls

-a

-l

-h

4 token(s) read

$ ls -alh

Line read: ls -alh

Token(s):

ls

-a

-l

-h

2 token(s) read

$ clear

Line read: clear

Token(s):

clear

1 token(s) read

$ exit ibrahim@ibrahim-latech:~$

Note that the first and last lines are not part of program output (i.e., they are of the terminal session launching the program and after its exit). The program prompts the user (with a simple prompt containing just a $ followed by a space) and accepts user input until the command exit is provided, at which point it exits the program and returns to the terminal. For all other user input, it first outputs the string Line read: followed by the user input provided (on the same line). On the next line, it outputs the string Token(s):. This is followed by a list of the tokens in the input provided, each placed on a separate line and indented by a single space. For our purposes, a token is any string in the user input that is delimited by a space (i.e., basically a word). The string n token(s) read is then outputted on the next line (of course, n is replaced with the actual number of tokens read). Finally, a blank line is outputted before prompting for user input again. The process repeats until the command exit is provided. Hints: (1) An array of 256 characters for the user input should suffice (2) To get user input, fgets is your friend (3) To compare user input, strcmp is your friend (4) To tokenize user input, strtok is your friend Turn in your .c source file only that is compilable as follows (filename doesn't matter): gcc -o prog1 prog1.c Make sure to comment your source code appropriately, and to include a header providing your name.

Solutions

Expert Solution

Complete code in C:-

#include <stdio.h>
#include <string.h>

// Main function
int main(void) {
   // 'token' variable keeps count of tokens
   int tokens;
   // 'string[]' array will take user input.
   char string[256];
   // Taking user input.
   fgets(string, 256, stdin);
   // Printing output.
   printf("$ ");
   // This while loop will run until "exit" entered as input by user.
   while(strcmp(string, "exit\n") != 0) {
       // Printing output.
       printf("Line read: %sToken(s):", string);
       // Fetching first token from input string.
       char *token = strtok(string, " ");
       // Initializing 'tokens' as 0.
       tokens = 0;
       // Fetching all tokens from input string.
       while(token != NULL) {
           printf("\n%s", token);
           token = strtok(NULL, " ");
           tokens++;
       }
       // Printing output.
       printf("%d token(s) read\n", tokens);
       printf("$ ");
       // Again taking user input.
       fgets(string, 256, stdin);
   }
   return 0;
}

Screenshot of output:-


Related Solutions

Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
Write a program in c++ that prompts the user to input a coin collection of number...
Write a program in c++ that prompts the user to input a coin collection of number of quarters, dimes, nickels and pennies. The program should then convert the coin collection into currency value as dollars. The coin values should all be whole numbers and the resulting currency value should be displayed with two decimals. An example of user interaction is as follows: Coin Convertor Enter number of quarters: 3 Enter number of dimes: 1 Enter number of nickels: 4 Enter...
In C program #include<stdio.h> Write a program that prompts the user to input the elapsed time...
In C program #include<stdio.h> Write a program that prompts the user to input the elapsed time for an event in seconds. The program then outputs the elapsed time in hours, minutes, and seconds. Example (Numbers with underscore indicate an input): Enter the elapsed time in seconds: 9630 The elapsed time in seconds = 9630 The equivalent time in hours:minutes:seconds = 02:40:30 HINT: Pay attention to the printf format descriptors.
Write a complete C++ program that prompts the user for and takes as input, numbers until...
Write a complete C++ program that prompts the user for and takes as input, numbers until the user types in a negative number. the program should add all of the numbers together. Then if the result is less than 20 the program should multiply the result by 3, otherwise subtract 2 from the result. Finally, the program should printout the result.
Write a C++ program that prompts the user (or “Player 1”) to input an integer value...
Write a C++ program that prompts the user (or “Player 1”) to input an integer value between 1 and 3 (where 1=paper, 2=scissor, and 3=rock). This input should be passed into a string function called player_RPS(), and returns a string value indicating the selection of paper, scissors, or rock (as mentioned above). Next, the returned string value, along with a generated input from the computer, should be passed into a void function called RPS_comparison(), and determines whether the user’s input...
Write a program that prompts the user to input a string. The program then uses the...
Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning of your program and...
Write a program(C#) that prompts the user for her home planet. Based on the user's input...
Write a program(C#) that prompts the user for her home planet. Based on the user's input the program will display the following: Input: earth Message: earth. You are an Earthling and you have 10 fingers Input: VENUS Message: VENUS. You are a Venusian and you have 12 fingers Input: Mars Message: Mars. You are a Martian and you have 8 fingers any other input Message: I am sorry I don't know of that planet You may use either the ToUpper()...
JAVA: Write a program that prompts the user to input a type of medication and the...
JAVA: Write a program that prompts the user to input a type of medication and the output will be a list of side effects that can occur from that medication.
Write a java program that prompts the user to see if they wish to encode or...
Write a java program that prompts the user to see if they wish to encode or decode a message. (a) If they choose to encode a message: i. Ask them to enter the message and store it in a String variable. ii. Once the message is entered, ask them for a “shift” integer, similar to Lab 6. iii. Using that shift, move all alphabetic characters in the message (i.e. excluding spaces and punctuation) using char and int arithmetic. Be sure...
Write a program that uses input to prompt a user for their name and then welcomes...
Write a program that uses input to prompt a user for their name and then welcomes them. Note that input will pop up a dialog box. Enter Sarah in the pop-up box when you are prompted so your output will match the desired output.(In Python)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT