Thread: Unique Word count program

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    5

    Unique Word count program

    I am working on an assignment to count specific words in a file. I am able to copy the file into an structure of an array. I thought I would use strcmp and compare each member of the structure to the others, increase my count if they are equal etc.

    I ran into a snag becuase the strcmp function works with character pointers, and not structures.

    Any advice would be helpful. I am new to C and programing so I hope the code is easy to follow. The UniqueWord function at the bottom is where I am running into the problem with strcmp not working with the structure.

    Thanks

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<ctype.h>
    
    #define MAXWORDS 1000
    
    typedef struct
    {
    	char Word[88];
    
    }UniqueWords;
    
    char *GetWord(char *Source, char *Dest);
    
    UniqueWords WordCount(UniqueWords Source[1000]);
    
    int main(int c, char **v)
    
    {
    	char *LocationGetty = "C:\\Users\\svjefcoat\\Documents\\Visual Studio 2010\\Projects\\assignment 10\\Getty.txt";
    	UniqueWords Word[MAXWORDS];
    	UniqueWords WordCopy[MAXWORDS];
    
    	UniqueWords *pList;
    	
    	char line[99];
    	char Dest[39];
    	char *Read;
    	FILE *pStorage;
    	char *pDArray;
    	
    	pStorage = fopen(LocationGetty, "r"); //Location Getty
    	if(!pStorage)
    		return 0;
    	pList = Word;
    
    	while(1)
    	{
    		if(feof(pStorage))
    			break;
    		*line=NULL;
    
    		fgets(line,sizeof(line), pStorage);
    
    		pDArray = malloc(sizeof(line));
    
    		strcpy(pDArray, line); 
    
    		pDArray[(strlen(pDArray))+1] = '\0';
    
    		printf("%-s", line);
    
    		Read = pDArray;
    
    		for(;;)
    		{
    			Read = GetWord(Read, pList->Word);
    
    			if(*pList->Word=='\0')
    				break;
    
    			pList++;
    
    		}
    	}
    
    	WordCount(Word);
    
    	free(pDArray);
    
    	fclose(pStorage);
    
    					
    	getchar();
    
    	return 0;
    }
    
    char *GetWord(char *Source, char *Dest)
    {
    	// skip all leading space characters
    	while(*Source)
    	{
    		if(isspace(*Source))
    		{
    			Source++;
    			continue;
    		}
    		else
    			break;
    	}
    
    	while(*Source)
    	{
    		if(isspace(*Source)) 
    		{
    			*Dest = '\0';
    			return Source;
    		}
    		*Dest++ = *Source++;
    	}
    	*Dest = '\0';
    	return Source;
    }
    
    
    UniqueWords WordCount(UniqueWords Source[1000])
    {
    	 UniqueWords *pSource;
    	 UniqueWords *pSourceTest;
    	 int i;
    	 char *topSource;
    	 char *topSourceTest;
    
    	 pSource=Source;
    	 pSourceTest=Source;
    
    	 	
    	 i=0;
    
    	 for(;;)
    	 {
    	 if(*pSourceTest->Word=='\0') 
    		 break;
    
    	 if(strcmp(pSource, pSourceTest))
    		{
    		 i ++;
    		 
    		}
    	 pSourceTest ++;
    	 }
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Why do you need to compare strings?

    Just count the spaces and add 1...

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    Very interesting on the for loop for loop controlled with the feof. I was getting problems reading the whole file instead of a line at a time. I was getting a double line printed at the end of the file that should not have been there. That is why I switched to reading the file a line at a time.

    I am trying to read the text from a file and tell how many specific words are there.

    For example the are 20 occurrences of "The" in the national anthem. This is why I am comparing strings.

    Lots of things I have tried are having problems becuase of the structure.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Word count program
    By Jpeg6 in forum C Programming
    Replies: 1
    Last Post: 10-18-2010, 10:34 PM
  2. word count program not working
    By vsovereign in forum C Programming
    Replies: 5
    Last Post: 06-04-2010, 02:11 PM
  3. Count Number of Letters in a Word
    By quiksand in forum C Programming
    Replies: 10
    Last Post: 05-14-2010, 11:44 PM