Closed Thread
Results 1 to 4 of 4

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

  1. #1
    siren is offline Newbie
    Join Date
    Apr 2007
    Posts
    10
    Rep Power
    0

    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. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42
    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.

  4. #3
    edwar is offline Newbie
    Join Date
    Sep 2009
    Posts
    1
    Rep Power
    0

    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

  5. #4
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    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.
    sudo rm -rf /

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Word Count problem
    By Jesp in forum Java Help
    Replies: 7
    Last Post: 12-06-2010, 06:14 AM
  2. Word count program noobie help
    By code_prowannabe in forum C and C++
    Replies: 2
    Last Post: 11-21-2010, 06:47 PM
  3. program help asap please
    By uniQue in forum Pascal and Delphi
    Replies: 13
    Last Post: 09-23-2010, 01:44 PM
  4. Word Jumble program?
    By JewFro297 in forum C and C++
    Replies: 3
    Last Post: 01-20-2010, 10:29 AM
  5. need help with word count C++ program
    By waynejr25 in forum C and C++
    Replies: 2
    Last Post: 11-02-2007, 02:31 PM

Tags for this Thread

Bookmarks

Posting Permissions

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