Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- struct word
- {
- char *name;
- int number;
- };
- void print10 (struct word *array, int wordnum);
- int main ()
- {
- struct word *array;
- char *tmp;
- int ch = 1, i = 0, j = 0, wordnum = 0;
- array = malloc (sizeof (struct word) * 320);
- printf ("Input text:\n");
- while (ch != EOF)
- {
- tmp = malloc (32);
- ch = getchar ();
- while ((ch <= 'Z' && ch >= 'A') || (ch <= 'z' && ch >= 'a') || (ch <= '9' && ch >= '0'))
- {
- tmp [i] = ch;
- i++;
- ch = getchar ();
- }
- if (i != 0)
- {
- tmp [i] = 0;
- while (j < wordnum)
- {
- if (strcmp (array [j].name, tmp) == 0)
- {
- array [j].number++;
- break;
- }
- j++;
- }
- if (j == wordnum)
- {
- array [wordnum].name = tmp;
- array [wordnum].number = 1;
- wordnum++;
- }
- i = 0;
- j = 0;
- }
- }
- /* printf ("wordnum = %d\n", wordnum);*/
- printf ("\n");
- print10 (array, wordnum);
- for (i = 0; i < wordnum; i++)
- free (array [i].name);
- free (array);
- return 0;
- }
- void print10 (struct word *array, int wordnum)
- {
- int i, j, max = 0, maxnum;
- for (i = 0; i < 10; i++)
- {
- for (j = 0; j < wordnum; j++)
- {
- if (array [j].number > max)
- {
- max = array [j].number;
- maxnum = j;
- }
- j++;
- }
- printf ("%s %d\n", array [maxnum].name, max);
- array [maxnum].number = 0;
- max = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement