![DrippyGanja](/avatars/1530.jpg)
DrippyGanja
26.02.2020 •
Computers and Technology
Using C language, alter the program such that only the correct output is sent to the standard output stream (stdout), while error and help messages are sent to the standard error stream (stderr).
(Hint:use fprintf.)
See the expected output listed in the comment at the top of main.c for an example of what should go to stdout.
#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 < entry_count) {
if (!strcmp(entries[i].word, buffer))
entries[i].counter++;
i++;
}
line_count++;
}
return line_count;
}
void print_result(WordCountEntry entries[], int entry_count)
{
printf("Result:\n");
while (entry_count-- > 0) {
printf("%s:%d\n", entries->word, entries->counter);
}
}
void printHelp(const char *name)
{
printf("usage: %s [-h] ... \n", 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.\n",
prog_name, *argv);
}
} else {
if (entryCount < LENGTH(entries)) {
entries[entryCount].word = *argv;
entries[entryCount++].counter = 0;
}
}
argv++;
}
if (entryCount == 0) {
printf("%s: Please supply at least one word. Use -h for help.\n",
prog_name);
return EXIT_FAILURE;
}
if (entryCount == 1) {
printf("Looking for a single word\n");
} else {
printf("Looking for %d words\n", entryCount);
}
process_stream(entries, entryCount);
print_result(entries, entryCount);
return EXIT_SUCCESS;
}
Solved
Show answers
More tips
- F Food and Cooking When is Easter in 2011?...
- L Leisure and Entertainment Scrapbooking: What is it and Why is it Becoming More Popular?...
- H Horoscopes, Magic, Divination How to Cast a Love Spell on a Guy? Guide for Guys...
- F Family and Home How to Sew Curtain Tapes: Best Tips from Professionals...
- S Style and Beauty How are artificial nails removed?...
- S Style and Beauty How to Make Your Lips Fuller? Ideas and Tips for Beautiful Lips...
- F Food and Cooking The Disease That Haunted Abraham Lincoln...
- C Computers and Internet How to Get Rid of Windows Genuine Check?...
- H Health and Medicine How to perform artificial respiration?...
- S Style and Beauty Tricks and Tips: How to Get Rid of Freckles...
Answers on questions: Computers and Technology
- C Computers and Technology What are some Dos and Don ts for facial expression, Body movements and postures, gestures, Eye contact, touch, space and our voice either during a job interview or the workplace...
- C Chemistry Elements in group 2 are all called alkaline earth metals. What is most similar about the alkaline earth metals? how many protons and neutrons they have o which chemical properties...
- C Chemistry If 15.0 g CO2 (mm= 44.01 g/mol) has a volume of 0.30L at 27oC, what is the pressure in atm? Group of answer choices 27.97 atm 1230.9 atm 2.52 atm 325.0 atm...
- S Social Studies Some of the soldiers who returned from Iraq suffered from post-traumatic stress disorder (PTSD). Since the soldiers already have PTSD, doctors can t prevent the formation of painful...
- M Mathematics Please help need this to be right...
- M Mathematics It’s not C so I don’t which is the correct answer so can someone plz help me with this plzzz...
- M Mathematics I NEED THIS NOW PLS HELPPP :))The following graph describes function 1, and the equation below it describes function 2. Determine which function has a greater maximum value, and...
- M Mathematics What is the surface area of this design I need this ASAP...
- B Biology Which mineral is used in making cans and is found mainly in Australia? hematite bauxite tektite magnetite...
- B Business is an important marketing activity as it allows salespeople to represent the firm to customers, gather information on customer likes and dislikes, and provide market feedback on...
Ответ:
In if, the statements inside the if block executes if the expression is true. If the expression is false the next statement after the if block executes. In if else, the if block executes if the expression is true and if the expression is false the control is passed to the else block.
Explanation: