Thread: Strings and file word counting Program

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    166

    Strings and file word counting Program

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    #define FLUSH while (getchar() != '\n')
    #define MAXFILESIZE 100
    char getData(FILE* fp);
    
    int main(void)
    {
    FILE* fp;
    char filename[80];
    char terminate[5] = "QUIT";
    int quit = 1;
                   printf("\t\t Homework 4 - Pointers and Strings:\n");
                   do
                   {
                           printf("Enter a filename (Enter'QUIT' to quit):\n");
                           scanf("%s", filename);
                           FLUSH;
                           quit = strcmp(filename, terminate) == 0;
    
                           if(!quit)
                           {
                                   fp = fopen(filename, "r");
                                   if(fp)
                                   {
                                           printf("Open success!\n");
                                           getData(fp);
                                           fclose(fp);
                                   }
                                   else
                                   {
                                           printf("Failed...\n");
                                   }
    
                           }
                   }
                   while(!quit);
    }
    
    char getData(FILE* fp)
    {
    int i;
    char *ptemp;
    char **plist;
    char *list[MAXFILESIZE];
    char temp[100];
    char wordCounter;
           *list = (char *)malloc(MAXFILESIZE * sizeof(char));
           if(*list == NULL)
           {
           printf("NOT ENOUGH MEMORY!\n");
           exit(100);
           }
    while(fscanf(fp, "%[^ ' ' ^ \n]", temp));
           if(ispunct(temp[strlen(temp) - 1 ]))
           {
                   (temp[strlen(temp) - 1]) = '\0';
           }
           {
                   for(i = 0; i <strlen(temp); i++)
           {
                           temp[i] = tolower(temp[i]);
           }
           printf("%s\n", temp);
    
           }
    *list = temp;
    printf("%s\n", *list);
    }
    Ok, so the idea of this program is that I have a file. For example, one that has a song, row row row your boat....etc. And I need to count the number of words in that file. If the word repeats, then I count it the number of times it repeats. And then write the output to a file that would go something like:
    row 3
    your 1
    boat 1
    Also, the case should not affect it(I already took care of this) and the punctuation of the end( like the ',') should be removed(again, I took care of this).
    For starters, I'm having trouble reading in more than just the first word... Not sure how to make a loop that would work. Lastly, I have NO idea how to get the counter to work... Especially for repeating words(how to check it)...
    Anyway, thank you for reading this and for your time.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    to get more than one word, you would wrap lines 56 through 68 in an outer loop that has the exit condition you want, probably detecting end of file when fscanf returns EOF instead of 0 or 1.

    I would take a different approach to reading in the data. I would read a line at a time using fgets and then use strtok to separate words. with strtok you can specify all the punctuation and whitespace delimiters so that you only get words. check out the example in strtok - C++ Reference

    to count words, you will need a data structure that contains each word and a count of those words. when you find a word, you search the list, find the matching word and increment its count. if you don't find the word, add it to the list and set count to 1. there are a lot of choices of data structure. for a limited set of words, an array of structs with enough space for the max words you would find would be easier. otherwise you might need a linked list or something more complicated.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Quote Originally Posted by dmh2000 View Post
    to get more than one word, you would wrap lines 56 through 68 in an outer loop that has the exit condition you want, probably detecting end of file when fscanf returns EOF instead of 0 or 1.

    I would take a different approach to reading in the data. I would read a line at a time using fgets and then use strtok to separate words. with strtok you can specify all the punctuation and whitespace delimiters so that you only get words. check out the example in strtok - C++ Reference

    to count words, you will need a data structure that contains each word and a count of those words. when you find a word, you search the list, find the matching word and increment its count. if you don't find the word, add it to the list and set count to 1. there are a lot of choices of data structure. for a limited set of words, an array of structs with enough space for the max words you would find would be easier. otherwise you might need a linked list or something more complicated.
    Ok, I will try to create an outer loop. As for strtok, I'm not allowed to use it. Its for a class assignment and we haven't learned about it so our teacher wouldn't allow it.
    I'm still confused about the count though... List is my main array of pointers.... But I'm supposed to put each word into temp first. And this really confuses me. I'm struggling to see the logic from here on out. And then I have some pointers to access them, plus I have the counter array as well. I declared all of those things(arrays, pointers, etc), but I am not sure how to use them and for what. If someone could clear this up for me and explain step by step what goes where, then maybe I'll have an easier time writing the code. Sorry for all this trouble.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    What kind of outer loop would you suggest? I tried a do-while loop and said while(!EOF). But that didn't seem to do anything for me... But I don't know how else to state that I should loop something until fscanf return EOF.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Word Counting Program
    By psv1120 in forum C Programming
    Replies: 11
    Last Post: 02-23-2011, 03:57 PM
  2. word counting program
    By mashour06 in forum C Programming
    Replies: 2
    Last Post: 06-09-2009, 03:58 AM
  3. Replies: 2
    Last Post: 12-02-2007, 05:40 AM
  4. Word Counting Program Help
    By rdave1 in forum C++ Programming
    Replies: 1
    Last Post: 09-14-2005, 04:30 PM
  5. strtok - word counting from a file
    By |PiD| in forum C Programming
    Replies: 4
    Last Post: 11-15-2002, 04:16 AM