Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/* About this program: - This program counts words. - The specific words that will be counted are passed in as command-line arguments. - The

image text in transcribed

/* About this program: - This program counts words. - The specific words that will be counted are passed in as command-line arguments. - The program reads words (one word per line) from standard input until EOF or an input line starting with a dot '.' - The program prints out a summary of the number of times each word has appeared. - Various command-line options alter the behavior of the program.

E.g., count the number of times 'cat', 'nap' or 'dog' appears. > ./main cat nap dog Given input: cat . Expected output: Looking for 3 words Result: cat:1 nap:0 dog:0 */

#include #include #include #include "smp0_tests.h"

#define LENGTH(s) (sizeof(s) / sizeof(*s))

/* Structures */ typedef struct { char *word; int counter; } WordCountEntry;

int process_stream(WordCountEntry entries[], int entry_count) { short line_count = 0; char buffer[30];

while (gets(buffer)) { if (*buffer == '.') break; /* Compare against each entry */ int i = 0; while (i

void print_result(WordCountEntry entries[], int entry_count) { printf("Result: "); while (entry_count-- > 0) { printf("%s:%d ", entries->word, entries->counter); } }

void printHelp(const char *name) { printf("usage: %s [-h] ... ", name); }

int main(int argc, char **argv) { const char *prog_name = *argv;

WordCountEntry entries[5]; int entryCount = 0;

/* Entry point for the testrunner program */ if (argc > 1 && !strcmp(argv[1], "-test")) { run_smp0_tests(argc - 1, argv + 1); return EXIT_SUCCESS; }

while (*argv != NULL) { if (**argv == '-') {

switch ((*argv)[1]) { case 'h': printHelp(prog_name); default: printf("%s: Invalid option %s. Use -h for help. ", prog_name, *argv); } } else { if (entryCount

process_stream(entries, entryCount); print_result(entries, entryCount);

return EXIT_SUCCESS; }

Bug hunting 3) Explain and fix the logical flow bug within the switch statement. (What happens when the -h option is used?) 4 Explain and fix the argument parsing error. (Why is entrycount never zero?) 5) Fix print result() to print results correctly and in the same order as the words were specified on the command line. Explain your solution Bug hunting 3) Explain and fix the logical flow bug within the switch statement. (What happens when the -h option is used?) 4 Explain and fix the argument parsing error. (Why is entrycount never zero?) 5) Fix print result() to print results correctly and in the same order as the words were specified on the command line. Explain your solution

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advanced MySQL 8 Discover The Full Potential Of MySQL And Ensure High Performance Of Your Database

Authors: Eric Vanier ,Birju Shah ,Tejaswi Malepati

1st Edition

1788834445, 978-1788834445

More Books

Students also viewed these Databases questions

Question

Presentation Aids Practicing Your Speech?

Answered: 1 week ago