This blog is under construction

Thursday 18 July 2013

C program to print the frequency of words in a string

Write a C program to count the frequency of words in a string.


  #include <stdio.h>
  #include <string.h>

  int main() {
        char string[256], text[256], words[100][256], temp[256];
        int i, j, k, n, count;

        i = j = k = n = 0;

        /* get the input string from the user */
        printf("Enter your input string:");
        fgets(string, 256, stdin);
        string[strlen(string) - 1] = '\0';

        /* copying each and every word from the string */
        while (string[i] != '\0') {
                if (string[i] == ' ') {
                        words[j][k] = '\0';
                        k = 0;
                        j++;
                } else {
                        words[j][k++] = string[i];
                }
                i++;
        }

        words[j][k] = '\0';
        n = j;

        /* sort the words in the given string */
        for (i = 0; i < n; i++) {
                strcpy(temp, words[i]);
                for (j = i + 1; j <= n; j++) {
                        if (strcmp(words[i], words[j]) > 0) {
                                strcpy(temp, words[j]);
                                strcpy(words[j], words[i]);
                                strcpy(words[i], temp);
                        }
                }
        }

        printf("Frequency of words:\n");
        i = 0;

        /* find the frequency of each word and print the results */
        while (i <= n) {
                count = 1;
                if (i != n) {
                        for (j = i + 1; j <= n; j++) {
                                if (strcmp(words[i], words[j]) == 0) {
                                        count++;
                                }
                        }
                }

                /* count - indicates the frequecy of word[i] */
                printf("%s\t%d\n", words[i], count);

                /* skipping to the next word to process */
                i = i + count;
        }

        printf("\n");
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input string:apple bat apple ball bat ball cat rat cat ball
  Frequency of words:
  apple     2
  ball        3
  bat        2
  cat        2
  rat        1


1 comment: