+ Reply to Thread
Results 1 to 4 of 4

Thread: Need help w/ word count program (ASAP)

  1. #1
    Newbie siren is an unknown quantity at this point
    Join Date
    Apr 2007
    Posts
    10

    Need help w/ word count program (ASAP)

    I am in need for some assistance....I need to create a program that counts the number of words in certain program. The requirements for the program are
    1) The program first reads a text from a txt file.
    2)It counts how many times each word appears in the file
    3) It displays the word and how many times it appears at the end...

    sorry for the bad explanation. Here's an example.

    It would call up a txt file such as test.txt
    This is a dog.
    This is a cat.
    The dog loves the cat!!

    In this case it needs to count like so
    This=1
    is=2
    a=2
    dog.=1
    That=1
    cat.=1
    The=1
    dog=1
    loves=1
    the=1
    cat!!=1

    Although its an incomplete program, meaning that it counts "dog" and "dog." as two different words, my professor wants it to work out like so....

    Another requirement is I combine these two programs I've created before
    Code:
    #include<stdio.h>
    #define SENTENCE_MAX 80
    #define LIST_MAX 100
    
    int main(){
      int i,j;
      char ch, sentence[SENTENCE_MAX], Slist[LIST_MAX][SENTENCE_MAX];
      for(i=0;i<LIST_MAX;i++){
        Slist[i][0]=0;
      }
      printf("*------- Sentence List Program -------* \n");
      i=0;
      do{
        ch=getc(stdin);
        j=0;
        while((ch!=10)&&(ch!=EOF)&&(j<SENTENCE_MAX)){
          sentence[j]=ch;
          j++;
          ch=getc(stdin);
        }
        sentence[j]=0;
        strcpy(Slist[i],sentence);
        i++;
      }while(ch!=EOF);
      printf("*-------  Input file  ---------* \n");
      for(j=0;j<i;j++){
        printf("%s \n",Slist[j]);
      }
    }
    This is a 2d array program

    The other program I have to use is
    Code:
    #include<stdio.h>
    #define LIST_MAX 30
    int main(){
      int i, CHcount[LIST_MAX];
      char ch, CHlist[LIST_MAX];
    
      FILE *textin;
      
      for(i=0;i<LIST_MAX;i++){
        CHcount[i]=0;
        CHlist[i]=0;
      }
      
      textin = fopen("test.txt","r");
        do{
        ch=fgetc(textin);
        for(i=0;((???????)&&(???????));i++){
          if(ch==CHlist[i])
    	break;}
        
        if(CHcount[i]>0){
          ???????;
        }
        else{
         ?????? ;
         ??????;
        }
      }while((ch!=EOF));
      for(i=0;((???????)&&(???????));i++){ 
        printf("%c = %d \n",CHlist[i],CHcount[i]);
      }
      
      fclose(textin);
    }
    This program is suppose to count how many times the same letter appears in a text file...However, its still incomplete (the ??????) parts...

    I need to combine these two codes in order to create a program that counts how many times a word is repeated....I'm just having a very difficult time becuase everything is already fixed by the professor and not much freedom is given. Any help is appreciated!!!!!!!!

  2. #2
    Retired v0id is a glorious beacon of light v0id is a glorious beacon of light v0id is a glorious beacon of light v0id is a glorious beacon of light v0id is a glorious beacon of light v0id is a glorious beacon of light
    Join Date
    Apr 2007
    Posts
    2,978
    Blog Entries
    3
    I would do it a little different.
    I know you is required to use the two programs you've done before, but I will show you another slightly different solution to your problem, mostly because I will not do your homework. I've made a program, you can use it to look up how to do it, take snippets, or whatever you want.
    Code:
    /* 
    	This is the libraries we're using...
    	
    	stdio:
    		printf
    		getc
    		fopen
    		fclose
    		
    	string:
    		memset
    		strcmp
    		strcpy
    */
    #include <stdio.h>
    #include <string.h>
    
    /* Some defines used by the application */
    #define FILE_NAME		"test.txt"
    #define WORDS_AMMOUNT	100
    #define WORDS_LENGTH	50
    
    /* 
    	This is the structure, which holds the words 
    	
    	_word_ is the array of characters that holds
    	each string.
    	_ammount_ holds the ammount of occurrences of
    	the string.
    */
    typedef struct
    {
    	char word[WORDS_LENGTH];
    	int  ammount;
    } WORDS;
    
    /* Fills an array of characters with NUL (\0) */
    void clearString(char *string)
    {
    	memset(string, '\0', sizeof(string));
    }
    
    /* Initializes the list of words */
    void initialize(WORDS *list, int size)
    {
    	int x;
    	for(x = 0; x < size; x++)
    	{
    		clearString(list[x].word);
    		list[x].ammount = 1;
    	}
    }
    
    /* 
    	Check if the specific string (needle) is in the
    	array (haystack) - the array is a fixed size (ammount).
    */
    int inArray(WORDS *haystack, char *needle, int ammount)
    {
    	int i;
    	for(i = 0; i < ammount; i++)
    		if(strcmp(haystack[i].word, needle) == 0) /* We found it, return index-position */
    			return i;
    	return -1; /* We didn't find anything, return not-true */
    }
    
    int main()
    {
    	FILE  *fileHandle;
    	WORDS  wordList[WORDS_AMMOUNT];
    	char   string[WORDS_LENGTH];
    	int    stringCount  = 0;
    	int    listCount    = 0;
    	int    listIndex, i;
    	
    	/* Initialize list and clear string */
    	initialize(wordList, WORDS_AMMOUNT);
    	clearString(string);
    	
    	/* Create file stream */
    	fileHandle = fopen(FILE_NAME, "r");
    	do
    	{
    		/* We get one character from the filestream into the string */
    		string[stringCount] = getc(fileHandle);
    		
    		/* Check if the counter is at a space or newline, ... */
    		if(string[stringCount] == 32 || string[stringCount] == 10)
    		{
    			/* ... if it is, clear that character. */
    			string[stringCount] = '\0';
    			
    			/* Then check if the words already is in the list */
    			if((listIndex = inArray(wordList, string, listCount)) != -1)
    				wordList[listIndex].ammount++; /* if it is, increase the occurrence of the word... */
    			else
    			{
    				/* else insert the word and increase the counter of the list */
    				strcpy(wordList[listCount].word, string);
    				listCount++;
    			}
    			
    			/* When we are finished using the string we're clearing it, and set the counter to zero */
    			clearString(string);
    			stringCount = 0;
    		}
    		else
    			stringCount++;
    	} while(string[stringCount-1] != EOF);
    	
    	/* Close the file stream */
    	fclose(fileHandle);
    	
    	/* Print the result to the user */
    	for(i = 0; i < listCount; i++)
    	{
    		printf("WORD #%d\n", i);
    		printf(" - Word:          %s\n", wordList[i].word);
    		printf(" - Times occured: %d\n", wordList[i].ammount);
    		printf("\n");
    	}
    
    	return 0;
    }
    I've commented the code, so I'll not say anymore about it.

  3. #3
    Newbie edwar is an unknown quantity at this point
    Join Date
    Sep 2009
    Posts
    1

    Re: Need help w/ word count program (ASAP)

    do u know how could i sort the words on alphabet, i have to write the same program but i hae to sort the words on alphabet. any idea how to do that

  4. #4
    Code Warrior dargueta has much to be proud of dargueta has much to be proud of dargueta has much to be proud of dargueta has much to be proud of dargueta has much to be proud of dargueta has much to be proud of dargueta has much to be proud of dargueta has much to be proud of dargueta's Avatar
    Join Date
    Oct 2007
    Age
    19
    Posts
    2,847
    Blog Entries
    8

    Re: Need help w/ word count program (ASAP)

    Use the qsort library function and use strcmp() as the comparison function. You'll have to use a wrapper function for strcmp() since the prototypes are different:

    Code:
    int mycmpfunc(const void *a, const void *b)
    {
        return strcmp((const char *)a, (const char *)b);
    }
    Instead of passing strcmp() to qsort(), you would pass your wrapper function.
    Coding without commenting is like throwing up. You're making a mess that'll be fun to clean up.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Java:Tutorial - Tic-Tac-Toe
    By John in forum Java Tutorials
    Replies: 29
    Last Post: 03-12-2010, 11:53 PM
  2. Dictonary Program
    By programmer 101 in forum Java Help
    Replies: 9
    Last Post: 07-01-2007, 01:39 PM
  3. How do I Program another Program? !
    By bosco in forum General Programming
    Replies: 1
    Last Post: 06-15-2007, 11:15 AM
  4. Othello program!! 24 hours left!
    By siren in forum C and C++
    Replies: 1
    Last Post: 06-14-2007, 11:18 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts