Help with word counting problem

This is a discussion on Help with word counting problem within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to write a small program that counts the number of times a 1 letter, 2 letter, ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    10

    Help with word counting problem

    Hi,
    I am trying to write a small program that counts the number of times a 1 letter, 2 letter, 3 letter, 4 letter etc. words occur and print the frequency of their occurance as:

    1 letter - 1
    2 letter - 3
    3 letter - 8
    ...
    ...

    The program below seems to fail to print this. What am I doing wrong? Thank you for the help.

    Code:
    #include <stdio.h>
    
    int main()
    {
            int c;
            int inspace = 0;
            int wordlen = 0;
            int maxchar = 0;
            int words[] = {0};
            int i;
            int value = 0;
            while((c = getchar()) != EOF) {
                    if(c == ' ' || c == '\t' || c == '\n') {
                            if(inspace == 0) {
                                    inspace = 1;
                                    if(wordlen > 0) {
                                            printf("%d\n", wordlen);
                                            ++words[wordlen-0];
                                    }
                                    wordlen = 0;
                            }
                    }else{
                            if(inspace == 1) {
                                    inspace = 0;
                            }
                            ++wordlen;
                    }
            }
            for (i = 1; i<= 7; ++i) {
                    printf("word size = %d Frequency = %d\n", i, words[i]);
            }
            return 0;
    }
    After compiling, I am issuing the following command on my Linux machine at the command prompt:
    Code:
    ./a.out<text_file
    where text_file contains the following text:
    Code:
      a is it not not perfect perfect eightin
    Any help is appreciated. Thanks.

    Andrew.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    The most obvious problem is that you declare words to be an array of size 1.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    Quote Originally Posted by Tclausex View Post
    The most obvious problem is that you declare words to be an array of size 1.
    Hi Tclausex,
    Thank you.
    I have a somewhat decent Perl background and I am just learning C.

    In Perl, we can do something as:
    Code:
    my @words = ();
    to initialize my array with an empty list.

    How can I do something similar in C ?

    I do not know the array size in advance but I have to initialize the array with a '0'.

    Thank you.

    -A

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Yeah, Perl takes care of the memory allocation for you as you add to hashes and lists. In C, it's all on you.

    If you can't specify the array size at compile time, check out

    malloc - C++ Reference
    calloc - C++ Reference
    free - C++ Reference
    FAQ > Casting malloc - Cprogramming.com
    FAQ > malloc and calloc? - Cprogramming.com

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Quote Originally Posted by Tclausex View Post
    If you can't specify the array size at compile time, check out
    You are wrong.
    You can only specify the size of a classic array at compile time, you cannot it at runtime.
    At runtime you can specify the size of an VLA (only C99).
    Instead of an array you can use a memorybuffer and specify the size at runtime with realloc.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Quote Originally Posted by BillyTKid View Post
    You are wrong.
    You can only specify the size of a classic array at compile time, you cannot it at runtime.
    Note, the if in my statement and that it's directed at the OP's restrictions.


    But while we're revisiting this, why can't you specify the size of the words array at compile time? Wiki tells me that unless someone is going to sit down and spend a year typing in the chemical name for titin, I'm overly safe with an array of size 255. (But you wanted to learn dynamic memory right? )

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    I know what i say.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need a fix to word counting
    By Charak in forum C Programming
    Replies: 7
    Last Post: 02-25-2011, 05:55 AM
  2. Word counting
    By yuuh in forum C Programming
    Replies: 2
    Last Post: 08-09-2009, 11:47 PM
  3. Word Counting
    By cookie in forum C Programming
    Replies: 18
    Last Post: 06-17-2007, 12:31 PM
  4. Problem with letter and word counting
    By wordup in forum C Programming
    Replies: 3
    Last Post: 10-09-2002, 04:02 PM
  5. Word Counting
    By Achillles in forum C++ Programming
    Replies: 9
    Last Post: 09-11-2002, 02:09 PM

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