Solved

counting problem

Posted on 2006-10-19
5
406 Views
Last Modified: 2010-04-15

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);
  }
0
Comment
Question by:rmtogether
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17767225
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.
0
 
LVL 13

Assisted Solution

by:marchent
marchent earned 200 total points
ID: 17767882
//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~
0
 

Author Comment

by:rmtogether
ID: 17767951
hi, marchent

I copy your code but there is a complier error in sort function. could you please help ma about this. thanks!!
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 300 total points
ID: 17768152
  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()?
0
 

Author Comment

by:rmtogether
ID: 17768627
hi, sunnycoder

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C hashtable library 3 114
using pointers to pointers to write to a two dimensional array 16 102
C++ :Change value from  DisableCMD registry 4 80
What is atomic operation? 6 74
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question