45.0k views
2 votes
Write a C++ program that reads from the standard input and counts the number of times each word is seen. A word is a number of non-whitespace characters separated by whitespace.

After all input has been processed, print each word that was seen the largest number of times on a line by itself. The words should be printed in alphabetical order.

For example, with the input

Hello ... I said Hello are you there?

your program should print out

Hello

since it was the word that appeared most frequently in the input.

With the input

bow wow bow wow

the program should print out two lines:

bow

wow

Because both of those words appeared most frequently in the input.

If there is no input, your program should generate no output.

User Rbm
by
7.1k points

1 Answer

6 votes

Answer:

#include <stdio.h>

#include <string.h>

#include <ctype.h>

struct detail

{

char word[20];

int freq;

};

int update(struct detail [], const char [], int);

int main()

{

struct detail s[10];

char string[100], unit[20], c;

int i = 0, freq = 0, j = 0, count = 0, num = 0;

for (i = 0; i < 10; i++)

{

s[i].freq = 0;

}

printf("Enter string: ");

i = 0;

do

{

fflush(stdin);

c = getchar();

string[i++] = c;

} while (c != '\\');

string[i - 1] = '\0';

printf("The string entered is: %s\\", string);

for (i = 0; i < strlen(string); i++)

{

while (i < strlen(string) && string[i] != ' ' && isalnum(string[i]))

{

unit[j++] = string[i++];

}

if (j != 0)

{

unit[j] = '\0';

count = update(s, unit, count);

j = 0;

}

}

printf("*****************\\Word\tFrequency\\*****************\\");

for (i = 0; i < count; i++)

{

printf("%s\t %d\\", s[i].word, s[i].freq);

if (s[i].freq > 1)

{

num++;

}

}

printf("The number of repeated words are %d.\\", num);

return 0;

}

int update(struct detail s[], const char unit[], int count)

{

int i;

for (i = 0; i < count; i++)

{

if (strcmp(s[i].word, unit) == 0)

{

s[i].freq++;

return count;

}

}

/*If control reaches here, it means no match found in struct*/

strcpy(s[count].word, unit);

s[count].freq++;

/*count represents the number of fields updated in array s*/

return (count + 1);

}

User Jonas Czech
by
5.4k points