Thread: K&R pages 134-136 help with structures please

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

    K&R pages 134-136 help with structures please

    Hi,
    I am a beginner trying to learn C on my own. I am reading and running the programs in K&R. I am not able to clearly understand what is going on in the following program's getword function.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    #define MAXWORD 100
    #define NKEYS (sizeof keytab / sizeof (struct key))
    #define BUFSIZE 100
    
    struct key {
        char *word;
        int count;
    } keytab[] = {
    /*    "auto", 0, "break", 0, "case", 0, "char", 0, "const", 0, "continue", 0, "default", 0, "unsigned", 0, "void", 0, "volatile", 0, "while", 0, "if", 0, "for", 0, "return", 0, "define", 0,*/
        {"a3a^", 0}, {"b", 0}, {"c", 0}, {"d", 0}, {"e", 0}, {"f", 0}, {"g", 0}, {"h", 0}, {"i", 0}, {"j", 0}, {"k", 0}, {"l", 0}, {"m", 0}, {"n", 0}, {"o", 0}, {"p", 0}, {"q", 0}, {"r", 0}, {"s", 0}, {"t", 0}, {"u", 0}, {"v", 0}, {"w", 0}, {"x", 0}, {"y", 0}, {"z", 0}};
    
    int getword(char *, int);
    int binsearch(char *, struct key *, int);
    char buf[BUFSIZE]; /* buffer for ungetch */
    int bufp = 0; /* next free position in buf */
    
    /* count C keywords */
    int main()
    {
        int n;
        char word[MAXWORD];
        while (getword(word, MAXWORD) != EOF) {
            if (isalpha(word[0])) {
                if ((n = binsearch(word, keytab, NKEYS)) >= 0) {
                    keytab[n].count++;
                }
            }
        }
        for (n = 0; n < NKEYS; n++) {
            if (keytab[n].count > 0) {
                printf("%4d %s\n", keytab[n].count, keytab[n].word);
            }
        }
        return 0;
    }
    
    int binsearch(char *word, struct key tab[], int n)
    {
        int cond;
        int low, high, mid;
        
        low = 0;
        high = n - 1;
        while( low <= high) {
            mid = (low+high) / 2;
            if ((cond = strcmp(word, tab[mid].word)) < 0) {
                high = mid - 1;
            } else if (cond > 0) {
                low = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }
    
    int getword( char *word, int lim)
    {
        int c, getch(void);
        void ungetch(int);
        char *w;
        w = word;    
        c = getch();
        if(isspace(c)) {
            ;
        }
        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];
    }
    
    int getch(void) /* get a (possibly pushed back) character */
    {
        return (bufp > 0) ? buf[--bufp] : getchar();
    }
    
    void ungetch(int c) /* push character back on input */
    {
        if(bufp >= BUFSIZE) {
            printf("ungetch: too many characters\n");
        } else {
            buf[bufp++] = c;
        }
    }
    In the above code, the function getword is where I am getting confused. What is the purpose of
    Code:
     return c
    and
    Code:
     return word[0]
    in this function?

    When does
    Code:
     return c
    gets returned and when does
    Code:
     return word[0]
    gets returned?

    Thanks for all the help.

    -Andy

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    If I understand the code, they are just returning the first letter of the word, non-word, or EOF.

    The reason of the "return c" is that c is an int and can hold EOF while "word[0]" is only a char and can not hold EOF.

    Tim S.
    Last edited by stahta01; 12-26-2011 at 08:08 PM.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    In getword, you've changed
    Code:
    while(isspace(c=getch()))
    to a do-nothing if stmt ... change it back. Also, the keywords have to be in alphabetical order for a binary search.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    Quote Originally Posted by oogabooga View Post
    In getword, you've changed
    Code:
    while(isspace(c=getch()))
    to a do-nothing if stmt ... change it back. Also, the keywords have to be in alphabetical order for a binary search.
    Hi,
    I changed it back to
    Code:
    while(isspace(c=getch()))
    I see that the results are the same before and after changing the code back to
    Code:
    while(isspace(c=getch()))

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    I am still not understanding why we need the following code in the above program:
    Code:
        for ( ; --lim > 0; w++) {        
             if (!isalnum(*w = getch())) {             
                 ungetch(*w); 
                  break;         
             }     
        }
    I will appreciate if any one can help me understand.

    Thank you.

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    That code continues reading chars from stdin, copying them to w until it either runs out of space in w (lim==0) or encounters a non-alphanumeric char, which it ungets.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ manual pages?
    By KIBO in forum Linux Programming
    Replies: 5
    Last Post: 05-09-2009, 12:19 PM
  2. Access Web-Pages in C
    By bhupesh.kec in forum C Programming
    Replies: 1
    Last Post: 10-26-2007, 09:12 AM
  3. pages
    By lilhawk2892 in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2005, 01:18 PM
  4. embedding web pages
    By Devil Panther in forum Windows Programming
    Replies: 9
    Last Post: 01-14-2005, 09:37 AM
  5. ASP pages with VS.NET
    By Korn1699 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-27-2002, 09:42 AM