Thread: getword() from K&R page 136: for loop not clear

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    54

    getword() from K&R page 136: for loop not clear

    Hi!
    Below I am posting three functions: one getword(), and two
    supporting ones getch() and ungetch().
    I have issues with understanding the code.
    Please, help me to get through it.
    The word to use as an example: hello (as usually done).

    1) the first while loop skips through all space characters; and stops
    when getch() returns a non-space character; so at the end of the loop
    c = some non-space character, if there were characters considered as
    spaces at the beginning of the word.
    Code:
     while (isspace(c = getch ()))
            ;
    not relevant to the input "hello"
    2) if statement checks if c = EOF; if not, then w[0] = c, and
    *w is incremented to point to w[1].
    Code:
     if (c != EOF)
        {
            *w++ = c;
        }
    So, w[0] = 'h'
    3) next if statement checks if that c is alphabetical character; if it
    is not, then the word ends by assigning '\o' to w[1];
    not the case for hello.
    Code:
    if (!isalpha(c))
        {
            *w = '\0';
            return c;
        }
    4) for loop begins from w[1] (because w[0] = c), and it iterates
    until lim = 1, incrementing *w with each iteration, and with each iteration it prompts for the input by *w = getch().

    The inner
    Code:
    if (isalnum(*w = getch()))
    statement checks
    if *w (after it is assigned value it gets from getch) is alphanumeric.
    This part I don't understand:
    it says, that if *w value is alphanumeric, ungetch() (put into buffer)
    and break. Does it mean that even if there are alphabetic characters
    in the input, the program will ungetch them and not assign to the
    next *w? I understand I am wrong, but I read this if statement as the one that would eliminate the possibility of filling in the given array
    with alphabetical characters.
    What is my mistake?
    Code:
     for ( ; --lim > 0; w++)
        {
            if (isalnum(*w = getch()))
            {
                ungetch(*w);
                break;
            }
    Here are all 3 functions:
    Code:
    /* GETCH and UNGETCH functions */
    
    #define BUFSIZE 100
    
    /* buffer for ungetch */
    static char buf[BUFSIZE];
    
    /* next free position in buf */
    static int bufp = 0;
    
    /* get a (possibly pushed back) character */
    int getch (void)
    {
        return (bufp > 0) ? buf[--bufp] : getchar();
    }
    
    /* push character back on input */
    void ungetch (int c)
    {
        if (bufp >= BUFSIZE)
        {
            printf("ungetch: too many characters\n");
        }
        else
        {
            buf[bufp++] = c;
        }
    }
    
    /* getword: get next word or characer from input */
    int getword (char *word, int lim)
    {
        int c, getch(void);
        void ungetch(int);
        char *w = word;
        
        while (isspace(c = getch ()))
            ;
        if (c != EOF)
        {
            *w++ = c;
        }
        if (!isalpha(c))
        {
            *w = '\0';
            return c;
        }
        for ( ; --lim > 0; w++)
        {
            if (isalnum(*w = getch()))
            {
                ungetch(*w);
                break;
            }
        }
        *w = '\0';
        return word[0];
    }
    Thanks!

  2. #2
    Registered User
    Join Date
    Nov 2015
    Posts
    54
    Edit: I don't know how to delete posts. It seems I can't do that. I also can't edit the post. I have found my mistake:
    I incorrectly quoted the code, and that is why I couldn't understand it. Sorry.
    This post is irrelevant.
    Here is the correct piece:
    Code:
    for ( ; --lim > 0; w++)    {
            if (!isalnum(*w = getch())) //if not isalnum!
    
            {
                ungetch(*w);
                break;
            }
        }

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Don't delete the post. What you discovered is an important technique. You saw something you didn't understand, you quoted it (what you thought) on this forum and rearranged the parts to explain your understanding. Then you were able to answer your own question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 08-09-2015, 12:30 PM
  2. Vector::clear() won't clear
    By Ducky in forum C++ Programming
    Replies: 3
    Last Post: 06-16-2013, 04:23 AM
  3. Problem with K&R function getword (page 136) and '\n'
    By deckoff8 in forum C Programming
    Replies: 1
    Last Post: 04-07-2012, 01:23 AM
  4. getword and linked lists
    By nefsan in forum C Programming
    Replies: 7
    Last Post: 03-18-2009, 10:55 PM
  5. getword
    By john bim in forum C Programming
    Replies: 1
    Last Post: 04-23-2002, 09:25 AM

Tags for this Thread