Thread: Help with K&R Book Exercise

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    6

    Help with K&R Book Exercise

    Hi! I am learning C with the K&R book.
    I have a problem with exercise 1.13 from page 24 it says
    "Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical orientation is more challenging"
    I 'm not sure if i understand what it wants (my native language is not English)
    For example if a i write "Hello to all!" the program should print something like
    1 **** (Hello)
    2 ** (to)
    3 **** (all!)
    is this right? if so any hints?


    Much thanks,
    Cheers!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, that looks about right.

    What portion of this are you struggling with. Whilst this may not count as homework per se, me giving the answer directly would not be the right solution.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    First all, thanks for the quick answer!

    The problem i have is how to store the number of letters every word have. I can make an array to hold int's with the number of letters every word have and then do a for ..i am doing something like this..which is far from good

    Code:
    int c, i, j, count, word_number = 0, word[SIZE];
    
        while ((c = getchar()) != '1') {
            if (c != ' ' && c != '\t' && c != '\n') {
                ++count;
            }
            else {
                word[word_number] = count;
                count = 0;
                ++word_number;
            }
    
        }
        for (i = 0; i < word_number; ++i) {
            printf("%d: ", i+1);
            for (j = 0; j < word[i]; ++j) {
                printf("*");
            }
            printf("\n");
        }
    PS_ Ignore that 1 in the while condition, is because right now i am in windows..and Crt+Z don't works like in Linux.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Alejandrito View Post
    PS_ Ignore that 1 in the while condition, is because right now i am in windows..and Crt+Z don't works like in Linux.
    And why do you think so?

    Code:
    #include <stdio.h> 
    int main(int argc, char *argv[]) 
    { 
    	int c = getchar();
    	if(c == EOF)
    		puts("EOF detected");
    	else
    		printf("Char found - %c", c);
    	return 0; 
    }
    I press Ctrl+z,Enter
    Output
    Code:
    ^Z
    EOF detected
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    Quote Originally Posted by vart View Post
    And why do you think so?

    Code:
    #include <stdio.h> 
    int main(int argc, char *argv[]) 
    { 
    	int c = getchar();
    	if(c == EOF)
    		puts("EOF detected");
    	else
    		printf("Char found - %c", c);
    	return 0; 
    }
    I press Ctrl+z,Enter
    Output
    Code:
    ^Z
    EOF detected
    In linux, i don't need to press enter...
    Back to my question i did it.
    Code:
    #include <stdio.h>
    
    #define SIZE 100
    
    main()
    {
        int c, i, j, count = 0, word_number = 0, word[SIZE];
    
        while ((c = getchar()) != EOF) {
            if (c != ' ' && c != '\t' && c != '\n') {
                ++count;
            }
            else {
                word[word_number] = count;
                count = 0;
                ++word_number;
            }
    
        }
    
        for (i = 0; i < word_number; ++i) {
            printf("%d: ", i+1);
            for (j = 0; j < word[i]; ++j) {
                printf("*");
            }
            printf("\n");
        }
    }
    Cheers!

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    better make it
    int main(void)

    and add return 0; at the end

    to be more standard
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ascii backspace, K&R book exercises
    By jjohhn in forum C Programming
    Replies: 10
    Last Post: 12-29-2005, 08:33 AM
  2. book exercise
    By luigi40 in forum C# Programming
    Replies: 1
    Last Post: 11-13-2005, 11:28 AM
  3. getchar() problem from K&R book
    By anemicrose in forum C Programming
    Replies: 13
    Last Post: 04-04-2004, 11:06 PM
  4. Exercise from my book.
    By Vber in forum C Programming
    Replies: 5
    Last Post: 12-05-2002, 08:54 AM
  5. any recommended exercise book for C++?
    By gogo in forum C++ Programming
    Replies: 5
    Last Post: 11-07-2001, 04:44 PM