Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/*need to follow directions of comments*/ Expected output: Looking for 3 words Result: cat:1 nap:0 dog:0 */ #include #include #include #include smp0_tests.h /* B2 */

/*need to follow directions of comments*/

Expected output:

Looking for 3 words
Result:
cat:1
nap:0
dog:0
*/
#include
#include
#include
#include "smp0_tests.h"
/* B2 */
#define LENGTH(s) (sizeof(s) / sizeof(*s))
/* Structures */
/* These are like classes, the name of this structure is WordCountEntry
it can be instantiated like this
WordCountEntry nameOfStructureInstance;
*/
typedef struct {
char *word;
int counter;
} WordCountEntry;
/* Complete C4 and in this function
strtok() can be used to split a line into individual tokens.
For the separator characters we use whitespace (space and
tab), as well as the newline character ' '. We could also
trim the buffer to get rid of the newline, instead.
strtok returns NULL when no more tokens are available.
Google strtok line to learn more about how to use it */
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 < entry_count) {
/* B1: Google the meaning of the returned values of strcmp */
if (!strcmp(entries[i].word, buffer))
entries[i].counter++;
i++;
}
line_count++;
}
// Returns the number of lines processed.
return line_count;
}
/* Complete B5: introduce a temporary variable i and use it to access every index in entries */
void print_result(WordCountEntry entries[], int entry_count)
{
/* C1: send output to the right stream, use fprintf */
printf("Result: ");
/* fix this*/
while (entry_count-- > 0) {
printf("%s:%d ", entries->word, entries->counter);
}
}
void printHelp(const char *name)
{
/* C1: send output to the right stream, use fprintf */
/* C2: add instructions for the extra option -f */
printf("usage: %s [-h] ... ", name);
}
int main(int argc, char **argv)
{
const char *prog_name = *argv;
/* C3: make entries a pointer instead of an array */
WordCountEntry entries[5];
int entryCount = 0;
/* C1: create a variable to store the output stream to use, stdout by default
Hint: use the FILE data type and understand the stdout and stderr output streams*/
// FILE *output = ?? // Complete this stream variable definition (Note: this will not be a file)
/* Entry point for the testrunner program */
if (argc > 1 && !strcmp(argv[1], "-test")) {
run_smp0_tests(argc - 1, argv + 1);
return EXIT_SUCCESS;
}
/* C3: allocate (potentially) a little more memory than strictly
necessary, thus avoiding extensive modifications to the code below. Hint: use malloc */
/* B4: ignore the program name in argv */
while (*argv != NULL) {
if (**argv == '-') {
switch ((*argv)[1]) {
/* C2: -fFILENAME switch: open FILENAME and set it as the output
stream */
/* B3: fix the logical flow error in the switch*/
case 'h':
printHelp(prog_name);
default:
/* C1: send output to the right stream, use fprintf */
printf("%s: Invalid option %s. Use -h for help. ",
prog_name, *argv);
}
} else {
/* C3: the LENGTH macro will not work anymore, since entries will be a pointer, not an array */
if (entryCount < LENGTH(entries)) {
entries[entryCount].word = *argv;
entries[entryCount++].counter = 0;
}
}
argv++;
}
if (entryCount == 0) {
/* C1: send output to the right stream, use fprintf */
printf("%s: Please supply at least one word. Use -h for help. ",
prog_name);
return EXIT_FAILURE;
}
/* C1: send output to the right stream */
if (entryCount == 1) {
printf("Looking for a single word ");
} else {
printf("Looking for %d words ", entryCount);
}
/* You may modify the parameter list of any of these functions */
process_stream(entries, entryCount);
print_result(entries, entryCount);
// MANDATORY! FREE MEMORY, CLOSE FILES, STREAMS, etc.
return EXIT_SUCCESS;

}

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

Microsoft SQL Server 2012 Unleashed

Authors: Ray Rankins, Paul Bertucci

1st Edition

0133408507, 9780133408508

More Books

Students also viewed these Databases questions