title: C Program to count the word frequency in a text file
body: <p>I need to be able to write a code in C programming that can read the text file and find how many of each word there is and output the word and how many times it occurs. Right now I have code that will print out each word and how many times it occurs, but I need it to print in alphabetical order and to ignore the uppercase letters. For example, "It" and "it" should be counted as the same word. I'm not sure where in my code to include the revisions. Below is an example of my code.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  int main(int argc, char *argv[])
  {
      if (argc == 1) {
          printf("The input file name has not been provided\n");
      } else if (argc == 2) {
          FILE *f = fopen(argv[1], "rb");
          fseek(f, 0, SEEK_END);
          long fsize = ftell(f);
          fseek(f, 0, SEEK_SET);
  
          char *str = malloc(fsize + 1);
          fread(str, fsize, 1, f);
          fclose(f);
  
          str[fsize] = 0;
          int count = 0, c = 0, i, j = 0, k, space = 0;
          char p[1000][512], str1[512], ptr1[1000][512];
          char *ptr;
          for (i = 0; i &lt; strlen(str); i++) {
              if ((str[i] == ' ') || (str[i] == ',') || (str[i] == '.')) {
                  space++;
              }
          }
          for (i = 0, j = 0, k = 0; j &lt; strlen(str); j++) {
              if ((str[j] == ' ') || (str[j] == 44) || (str[j] == 46)) {
                  p[i][k] = '\0';
                  i++;
                  k = 0;
              } else
                  p[i][k++] = str[j];
          }
          k = 0;
          for (i = 0; i &lt;= space; i++) {
              for (j = 0; j &lt;= space; j++) {
                  if (i == j) {
                      strcpy(ptr1[k], p[i]);
                      k++;
                      count++;
                      break;
                  } else {
                      if (strcmp(ptr1[j], p[i]) != 0)
                          continue;
                      else
                          break;
                  }
              }
          }
          for (i = 0; i &lt; count; i++) {
              for (j = 0; j &lt;= space; j++) {
                  if (strcmp(ptr1[i], p[j]) == 0)
                      c++;
              }
              printf("%s %d \n", ptr1[i], c);
              c = 0;
          }
      }
      return 0;
  }
  </code></pre>