Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a computer program as a Win32 console application in C, not C++ to count t

ID: 3767853 • Letter: W

Question

Write a computer program as a Win32 console application in C, not C++ to count the number times every unique word occurs inside an input text file. The program shall then print the word that occurred most frequently in input file, and the number of times it occurs, in a brief statement on the computer screen.
The name of your source program shall be p6.c and that of your executable program shall be p6.exe.

You program shall receive information from the user as a command-line argument. For example, a typical command line to start your program (from a command prompt) might be L:>p6 testfile.txt

Your program shall NOT seek information from the user such as by a call to scanf().

Your program shall NOT print information to the screen except as required herein.

If your program is started without any command-line arguments, it shall print the following line to the screen and terminate execution: EE 233 Fall 2015 P6, <your Blazer id>, <your name> (Fill in your real name in place of <your name> and be sure to follow the output with a newline. Do NOT use the “greater-than” and “less-than” symbols in your actual output) If your program is started with at least one command-line argument, it shall NOT print the line given above but shall proceed with normal execution.

The first command-line argument (after the program name itself) will be the name of the text file for your program to read. The lines in this file may of any length.

If the file supplied as a command-line argument cannot be opened, your program shall write an error message to the screen and terminate execution. In a similar manner, your program shall properly handle all error conditions that might arise. Your program shall read the input text file and count the number of times each unique word occurs. A word is defined as a sequence of characters that is bounded by white space and that contains no characters other than letters of the alphabet (no number or punctuation, etc.). Two words that differ only in the case (upper or lower) of some of their characters are considered to be the same word. A word will be no longer than 30 characters.

Your program shall be able to count an “indefinite” number of different words. Your program shall not have an array or an array of structures in which to store the words. If there is an array in your program larger than what is needed to hold one word, you will receive zero credit for this program. You shall store the words in a linear linked list of structures; each node (single structure) shall hold the information about one word (i.e. the word, and number of times it has occurred in the given input file).
After your program has read and processed the entire file, it shall write the following (single) line to the screen stating the overall result.
The word, <xxxxx>, occurred 0000 times in <file name>.
(Fill in the real file name in place of <file name> and be sure to follow the output with a newline. Do NOT use the “greater-than” and “less-than” symbols in your actual output)   

Explanation / Answer

The simplest way to solve the problem is :
1). Read all the words from the file and store them in an array of strings.
2). Sort the array of strings.
3). Run a while loop over the sorted array with a temp counter, and print the unique words & its count of occurrence.
---------------

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 5000 /* Declaring the maximum number of words in file as 5000 */

static int myCompare (const void * a, const void * b)
{
return strcmp (*(const char **) a, *(const char **) b);
}

void sort(const char *arr[], int n)
{
qsort (arr, n, sizeof (const char *), myCompare);
}

int main(int argc, const char* argv[])
{
if(argv[1] == NULL){ printf("EE 233 Fall 2015 P6, BLAZER_ID, NAME"); return; }
  
char *fname; /* temp variable for filename */
char *words[MAXSIZE]; /* array to store all the words of file */
char *temp_word; int temp_counter; /* Variables to be used for printing the output */
int i,j;

sprintf(fname, "%s", argv[1]); /* moving filename into temp variable */
FILE *fin = freopen(fname, "r", stdin);

/* read the file till EOF is reached */
i=0;
while(fin != EOF)
{
scanf("%s", words[i++]);
}

/* The value of `i` after above while loop gives the count of total words in the text file */
/* Calling sort function to sort the array of words */
sort(words, i);

/* Printing the ouput */
for(j=0; j<i; j++)
{
   sprintf(temp_word, "%s", words[j]);
   temp_counter=0;

   while(words[j] == temp_word)
   {
   temp_counter++; j++;  
   }
   j--;

   printf("Word %s occurred %d times in %s", temp_word, temp_counter, fname);
}

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote