word count program need a bit of help!

This is a discussion on word count program need a bit of help! within the C Programming forums, part of the General Programming Boards category; I have wrote a code to have the user input a filename and then the amount of words in the ...

  1. #1
    Unregistered
    Guest

    Question word count program need a bit of help!

    I have wrote a code to have the user input a filename and then the amount of
    words in the file will be displayed.
    However i want it to recognise the delimiters space, comma, fullstop or
    newline and not count them as words eg. aaa .bb.ccc has 3 words
    I have included the full code without it counting delimiters below. I have
    tried to add the following line to have it recognise the delimeters and not
    count but it doesnt work:

    if ((inword != '\n' || ',' || '.' || ' '))
    word_count++;

    any ideas what im doing wrong?

    also i know i can use strtok but i want to do it without this!

    #include <stdio.h>
    #include <stdlib.h>
    #define MAX_LENGTH 256

    int main(void)
    {
    FILE *fp;
    char ch, filename[40];
    int word_count=0;
    char inword[MAX_LENGTH];


    /*get file name and open*/

    while(1) {
    printf("\nEnter a filename: ");
    scanf("%s", filename);
    /*check if the file is valid*/

    if( (fp = fopen(filename, "r" )) !=NULL )
    {
    printf("\n Succesfully opening %s. \n", filename);
    break;
    }
    else
    {
    fprintf(stderr, "\n error opening file %s.\n", filename);
    puts("Enter x to exit, any other to try again.");
    ch=getchar();
    ch=getchar();
    if( ch == 'x' )
    exit(0);
    else
    continue;
    }
    }


    /*count the words in the file*/

    while (!feof(fp) ) {
    fscanf( fp, "%s", inword);
    word_count++;
    }
    printf("there are %d word(s) in the file %s\n", word_count, filename);

    /*close file*/

    fclose(fp);

    return 0;

    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > if ((inword != '\n' || ',' || '.' || ' ')) word_count++;

    Is 'inword' a single character? If not then this comparison won't work.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    Is this a part 2 of this http://www.cprogramming.com/cboard/s...threadid=15710

    If you want to compare you need to do
    f (inword != '\n' ||inword != ',' ||inword != '.')
    {
    word_count++;
    {

  4. #4
    Unregistered
    Guest
    the link you posted is not related to this post at all. And even adding in that line of code it still isnt counting .'s or commas as sepearating words eg a.test file counts as two words where it should be 3

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Unregistered
    the link you posted is not related to this post at all. And even adding in that line of code it still isnt counting .'s or commas as sepearating words eg a.test file counts as two words where it should be 3
    Did you even read my post?

    inword IS AN ARRAY! YOU CANNOT DO A COMPARISON THAT WAY!

    You have to compare a single character at a time. You are trying to compare an array with a single character. This is your problem.

    Furthermore, your comparison is TOTALLY WRONG.

    if( inword[x] == '\n' || inword[x] == ',' || ...and so on ... )

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Unregistered
    Guest
    ok well im just trying to figure out how i would go about doing it.
    If i was to change inword for ch would i be getting on the right lines?
    you say im going completly wrong can you give me any ideas as to how, and any siggestions for the right way?
    thank you

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There are a few ways to do it:

    a) read a line at a time, then go through and count punctuation/spaces
    b) read a character at a time

    Reading a character at a time is probably the simplest.
    Code:
    int current, prev = ' ';
    while we're not done with the file:
        current = fgetc( fp );
        if( current == EOF )
            done with file
        else
            if ( current != space or punctuation ) continue
            else
            if ( previous == space or punctiuation ) increment word count
        prev = current
    Pretty easy.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User Led Zeppelin's Avatar
    Join Date
    Mar 2002
    Posts
    17
    You may find it easier to define your whitespace/punct. marks, that way it is easier to refer to and makes for neater code. Anyway thats my 2 cents. I assume you know defined statements are placed in the preprocessor directives. Anyway, you got 3 cents.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help me out Unrgent wit a cross word program
    By rags in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 11:12 AM
  2. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21