Using C language add support for matching arbitrary numbers
Using C language, add support for matching arbitrary numbers of words, not just 5. (Hint: use malloc, and don\'t worry too much about memory efficiency).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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:\ \");
while (entry_count-- > 0) {
printf(\"%s:%d\ \", entries->word, entries->counter);
}
}
void printHelp(const char *name)
{
printf(\"usage: %s [-h] <word1> ... <wordN>\ \", 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 < 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.\ \",
prog_name);
return EXIT_FAILURE;
}
if (entryCount == 1) {
printf(\"Looking for a single word\ \");
} else {
printf(\"Looking for %d words\ \", entryCount);
}
process_stream(entries, entryCount);
print_result(entries, entryCount);
return EXIT_SUCCESS;
}
Solution
The following is the program with required changes using malloc function:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include \"smp0_tests.h\"
/* Structures */
typedef struct {
char *word;
int counter;
} WordCountEntry;
//definition of the function process_stream()
int process_stream(WordCountEntry entries[], int entry_count)
{
//define the variables
short line_count = 0;
char buffer[30];
while (fgets(buffer, sizeof(buffer), stdin))
{
//check if the word has \'.\'
if (*buffer == \'.\')
break;
//find the length of the word
size_t len = strlen(buffer);
//check for the new line
if (buffer[len - 1] == \'\ \')
// assign 0
buffer[--len] = 0;
/* Compare against each entry */
if (!strcmp(entries[line_count].word, buffer))
//increment the count
entries[line_count].counter++;
if (++line_count == entry_count)
break;
}
//return the line_count
return line_count;
}
//definition of the function print_result()
//it uses printf to print the result
void print_result(WordCountEntry entries[], int entry_count)
{
printf(stdout, \"\ Result:\ \");
for (int i = 0; i < entry_count; i++) {
printf(\"%s:%d\ \", entries[i].word, entries[i].counter);
}
}
//definition of the function printHelp()
//it uses printf to print the Help
void printHelp(const char *name)
{
printf(stderr, \"usage: %s [-h] [-f FILENAME] <word1> ... <wordN>\ \", name);
}
//definition of the function main()
int main(int argc, char **argv)
{
const char *prog_name = *argv;
//Add support for matching arbitrary number of words, not just 5.
int size = malloc(sizeof(int), argc);
WordCountEntry entries[size];
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;
}
for (int i = 1; i < argc; i++)
{
if (*argv[i] == \'-\')
{
switch (argv[i][1])
{
// h to print help message.
// printHelp()
case \'h\':
printHelp(prog_name);
break;
case \'f\':
// f to open file
// freopen()
freopen((*argv)[2], \"w\", stdout);
break;
default:
printf(\"%s: Invalid option %s. Use -h for help.\ \", 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.\ \",
prog_name);
return EXIT_FAILURE;
}
if (entryCount == 1) {
printf(\"Looking for a single word\ \");
}
else {
printf(\"Looking for %d words\ \", entryCount);
}
process_stream(entries, entryCount);
print_result(entries, entryCount);
return EXIT_SUCCESS;
}




