Thread: hi, i got a question please

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    1

    hi, i got a question please

    so, im learning a bit of c, and going through the k and r book, i got to this question and im stuck

    its the one about write a program to print a histogram of the word lengths in its input (horizontal of course)

    thing is, i did get it to work, but then i decided to make it check the text first, to get the largest word, then declare the array to the correct size, before executing the part about counting the words and the printf stuff

    before i added the while statement to get the largest word stuff, it DID work !!!! now it doesnt

    can anyone see why ? i been staring at it since yesterday


    Code:
    #include <stdio.h>
    
    
    int main () {
        
        int wordsize = 0;
        int wordsizecheck = 0;
        int largestword = 0;
        int pl = 0;
        int c, i = 0;
        
        
    
    
        
        while ((c = getchar()) != EOF) { /*to find the largest word in the input, seems to work */
            if (c == ' ' || c == '\t' || c == '\n') {
                if (wordsizecheck > largestword) {
                    largestword = wordsizecheck;
                }
                wordsizecheck = 0;
            }
            else {
                ++wordsizecheck;
                if (wordsizecheck > largestword) {
                    largestword = wordsizecheck;
                }
            }
    
    
        }
        
        printf("largest word = %d\n", largestword); /* to check it worked */
        
        int wordlengths[largestword]; /*set the array*/
        
        
        for (i = 0; i < largestword; ++i) {
            wordlengths[i] = 0;
        }
        
        
        while ((c = getchar()) != EOF) {
            /*count the words of a certain length (this worked before i added the stuff above*/
            if (c == ' ' || c == '\t' || c == '\n') {
                ++wordlengths[wordsize - 1];
                wordsize = 0;
            }
            else {
                ++wordsize;
            }
        }
        if ((c = getchar()) == EOF) {
            ++wordlengths[wordsize - 1];
        }
        
        for (i = 0; i < largestword; ++i) { /*to check array, seems to indicate problam*/
            printf("array value %d = %d\n", i, wordlengths[i]);
        }
            
        for (i = 0; i < largestword; ++i) { /* print histogram ( worked before :( */
            printf("%4d | ", i + 1);
            for (pl = 0; (pl <= wordlengths[i]) && (wordlengths[i] != 0) ; ++pl) {
                printf("*");
            }
            printf("\n");
        }
        return 0;
    }
    any help id really appreciate, or any criticisms also, thanks

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    After the first loop, you have effectively read all the input (EOF returned from getchar()) ... so the subsequent attempts to read all return EOF.

    You're trying to read the input twice. You can't easily do that with streams (stdin used by getchar): imagine stdin is like a water tap ... you can't have the same water flow through the tap twice.

    Solution: read the data once only, keep a variable for the longest word, and use big enough arrays (or malloc(), realloc(), and free()).

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    This could as simple as reading whole string or a word using fgets and process them as a whole word instead of a char. Could be far much better! And also your way of reading char by char has flawed as you are reading them twice and expecting to have the same values. Which will not be the case. Why not do the following to get read the values

    Code:
    while(  fgets( str, 80, STDIN) != NULL )
    {
        copy the read string into another string
    }
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  2. *szString = things question/memory question
    By Jang in forum C Programming
    Replies: 3
    Last Post: 01-20-2011, 04:59 AM
  3. Newbish Question file reading question....
    By kas2002 in forum C Programming
    Replies: 23
    Last Post: 05-17-2007, 12:06 PM
  4. java question, how do you interpret this question.
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 11-02-2006, 10:30 AM
  5. Self regiserting DLLs question and libraries question.
    By ApocalypticTime in forum Windows Programming
    Replies: 2
    Last Post: 03-22-2003, 02:02 PM