We help IT Professionals succeed at work.

counting problem

rmtogether
rmtogether used Ask the Experts™
on

I have a program to count C keywords (if, else, while, chr,return...etc). there is no compile error, but the output result is wrong... Could someone help me debug the problem.

the execute output like below, there is alwasys has 2 while, but I am sure my test file (xxx.c) has many other C keywords. Could someone help me this?

a.out < xxx.c
  0 break
  0 else
  0 char
  0 const
  0 continue
  0 default
  0 unsigned
  0 void
  0 volatile
  2 while

---------------code--------------------------------------------
   #include <stdio.h>
   #include <ctype.h>
   #include <string.h>

   #define MAXWORD 29
   #define NKEYS 11
   #define BUFSIZE 100
   #define LETTER 'a'
   #define DIGIT '0'
   
   
   struct key {
       char *word;
       int count;
   }
   
   keytab[] = {
       "if", 0,
       "break", 0,
       "else", 0,
       "char", 0,
       "chr", 0,
       "continue", 0,
       "default", 0,
       "unsigned", 0,
       "void", 0,
       "return", 0,
       "while", 0
   };

 

   char buf[BUFSIZE];    /* buffer for ungetch */
   int bufp = 0;         /* next free position in buf */
   int getword(char *, int);
   int binsearch(char *, struct key *, int);
   char *keyword[NKEYS];
   int keycount[NKEYS];
   
   
   /* count C keywords */
   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;
   }


   /* binsearch:  find word in tab[0]...tab[n-1] */
   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;
   }
 
 
 
  getword (w,lim)
  char *w;
  int lim;
 
  {
 
  int c, t;
  if (type(c=*w++=getch())!=LETTER) {
    *w='\0';
    return(c);
  }
  while (--lim >0){
    t=type(c=*w++=getch());
    if (t!=LETTER && t!=DIGIT){
        ungetch(c);
        break;
   
    }
 
  }
 
 *(w-1)='\0';
 return (LETTER);
 
 
 }
 
 
 
    /* getword:  get next word or character 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];
   }

*/


   int getch(void)  /* get a (possibly pushed-back) character */
   {
       return (bufp > 0) ? buf[--bufp] : getchar();
   }

   ungetch(int c)   /* push character back on input */
   {
       if (bufp >= BUFSIZE)
           printf("ungetch: too many characters\n");
       else
           buf[bufp++] = c;
   }


  type (c)
  int c;
  {
    if (c>= 'a' && c <='z' || c>='A' && c<='Z')
        return(LETTER);
    else if (c>='0' && c<='9')
        return (DIGIT);
    else
        return (c);
  }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2006

Commented:
For binary search to work, your tab array must be sorted on keyword field. Also your get word function is unneccessarily complicated .. a simlpe fgets with sscanf would have served the purpose.
Commented:
//i just add a sort function and delete some unnessarry codes

   #include <stdio.h>
   #include <ctype.h>
   #include <string.h>

   #define MAXWORD 29
   #define NKEYS 11
   
   struct key {
       char *word;
       int count;
   }
   
   keytab[] = {
       "if", 0,
       "break", 0,
       "else", 0,
       "char", 0,
       "chr", 0,
       "continue", 0,
       "default", 0,
       "unsigned", 0,
       "void", 0,
       "return", 0,
       "while", 0
   };


   int binsearch(char *, int);
   void sort(int);
   char *keyword[NKEYS];
   int keycount[NKEYS];
   
   
   /* count C keywords */
   int main()
   {
       int n;
       char word[MAXWORD];
         sort(NKEYS);
       while (gets(word) != NULL)
           if (isalpha(word[0]))
               if ((n = binsearch(word, 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;
   }

void sort(int n) /*sort the keytab[] ascending order */
{
      struct key temp;
      for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                  if(strcmp(keytab[i].word,keytab[j].word ) < 0)
                  {
                        temp.word = keytab[i].word ;
                        keytab[i].word = keytab[j].word;
                        keytab[j].word = temp.word;
                        temp.count = keytab[i].count;
                        keytab[i].count = keytab[j].count;
                        keytab[j].count = temp.count;
                  }
}

   /* binsearch:  find word in tab[0]...tab[n-1] */
   int binsearch(char *word,int n)
   {
       int cond;
       int low, high, mid;

       low = 0;
       high = n - 1;
       while (low <= high) {
           mid = (low+high) / 2;
           if ((cond = strcmp(word, keytab[mid].word)) < 0)
               high = mid - 1;
           else if (cond > 0)
               low = mid + 1;
           else
               return mid;
       }
       return -1;
   }

~marchent~

Author

Commented:
hi, marchent

I copy your code but there is a complier error in sort function. could you please help ma about this. thanks!!
Top Expert 2006
Commented:
  keytab[] = {
       "if", 0,
       "break", 0,
       "else", 0,
       "char", 0,
       "chr", 0, >>>> perhaps this was a typo?
       "continue", 0,
       "default", 0,
       "unsigned", 0,
       "void", 0,
       "return", 0,
       "while", 0
   };

Sort this manually .. this is static data!!!

   keytab[] = {
       "break", 0,
       "char", 0,
       "continue", 0,
       "default", 0,
       "else", 0,
       "if", 0,
       "return", 0,
       "unsigned", 0,
       "void", 0,
       "while", 0
   };

Even if you were to write a routine for sorting why not use qsort()?

Author

Commented:
hi, sunnycoder

you are right if I  sort this manually it will work.