I am new in C programming and I would like help to make the following program:


A program that reads a file (file name you can get from command line arguments or
ask from the user) and produces output that counts the 10 most frequent words in the file
and their frequencies and percentages of all the words. A word with one alphabetic
characters separated by whitespace ( space, tab or newline) or punctuation marks such as
ʻ,ʼ , ʻ.ʼ etc.
Use functions to structure your program.

This is what I already have:

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

int main()
{
	/*local declarations*/
	char file_name[100] ;
    int count ;

    int update_counter = 0;

    FILE *inp1, *out1 ;

	/*init some stuff*/
    count=0 ;

   /*prompt the user*/
    printf("Enter Filename : ")  ;

	/* read the keyboard input*/
    fgets(file_name, sizeof(file_name), stdin);

	/*remove the newline character from the end of the input filename*/
	file_name[strlen(file_name) - 1] = '\0' ;

	/*open input file*/
	inp1 = fopen(file_name, "r+") ;
    if(inp1 == NULL)	
	{
	 fprintf(stderr, "can't open %s\n", file_name) ;
	 getchar();
     exit(EXIT_FAILURE) ;
	}

	/*open output file*/
	out1 = fopen("output.txt", "r") ;
    if(out1 == NULL)	
	{
	 fprintf(stderr, "can't open %s\n", "output.txt") ;
	 exit(EXIT_FAILURE) ;
	}

    /* count the words in the input file*/
    for ( ;; )
    {
       int ch = fgetc(inp1);
       if ( ch == EOF )
       {
          break;
       }
   if ( isalpha(ch) || isdigit(ch) || ispunct(ch))
       {
          update_counter = 1;
         
       }
       if ( isspace(ch) && update_counter )
       {
          count++;
          update_counter = 0;
       }
       if (update_counter) {

}

    }
 
	/*echo the word count for the file*/
    printf("\nFile %s contains %d words \n\n", file_name, count) ;
 
	/*write the word count to the output file*/
	fprintf(out1,"File %s contains %d words \n", file_name, count) ;

	/* we're done...close files*/
	fclose(inp1) ;
	fclose(out1) ;

  getchar();
}

Edited 6 Years Ago by WaltP: Added CODE Tags, Removed Color tags

please use [code=c] [/code] tags to post code, also please come to us with a specific problem, rather than asking us to program you homework

Welcome to the forum, Ndovu. :)

When you post code ALWAYS highlight it, and click on the [code] icon at the top of the advanced editor window. That will keep your code, looking good, and easy to study.

And please - just black. Save the red or green for a special highlight for some block or line of code.

So your algorithm here is to first, break up the text into it's words, and write them out to a file. Then what?

What hints did your teacher give you for this assignment?

I'd be tempted to put each word into a 2D array, and then sort them. Then work on the stats needed (since repeated words would be right next to each other then).

It's good to post up your code, so we can see what you're talking about, but it's just as important to ask the specific questions that relate to C. The broad questions/statements like "This doesn't work", really aren't that helpful. You know your teacher, have class notes and reference books you've been reading (hopefully), etc. For that reason the idea department on these programs, kind of falls into your territory, by default.

Think about it, and pop back when you need us - we're here. ;)

If nothing else comes to mind, ask yourself "How would I do this by hand, with paper and pencil". The answer is almost always easier, and can translate into a pseudo-code and then into a program.

This article has been dead for over six months. Start a new discussion instead.