Advertisement
Guest User

keyword search

a guest
Dec 4th, 2012
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.93 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. #include<string.h>
  4.  
  5. #include<ctype.h>
  6.  
  7. struct key {
  8.  
  9.     char *word;
  10.  
  11.     int count;
  12.  
  13.              };
  14.  
  15. struct key keytab[] = {
  16.  
  17.         "auto",0,
  18.         "break",0,
  19.         "case",0,
  20.         "char",0,
  21.         "const",0,
  22.         "continue",0,
  23.         "default",0,
  24.         "do",0,
  25.         "double",0,
  26.         "else",0,
  27.         "enum",0,
  28.         "extern",0,
  29.         "float",0,
  30.         "for",0,
  31.         "goto",0,
  32.         "if",0,
  33.         "int",0,
  34.         "long",0,
  35.         "register",0,
  36.         "return",0,
  37.         "short",0,
  38.         "signed",0,
  39.         "sizeof",0,
  40.         "static",0,
  41.         "struct",0,
  42.         "switch",0,
  43.         "typedef",0,
  44.         "union",0,
  45.         "unsigned",0,
  46.         "void",0,
  47.         "volatile",0,
  48.         "while",0
  49.  
  50.                     };
  51.  
  52. #define NKEYS (sizeof keytab / sizeof(struct key))
  53.  
  54. /*binary search*/
  55.  
  56. int binsearch (char * word, struct key tab[], int n) {
  57.  
  58.     int cond;
  59.  
  60.     int low, high, mid;
  61.  
  62.     low = 0;
  63.  
  64.     high = n -1;
  65.  
  66.     while (low <= high) {
  67.    
  68.         if ((cond = strcmp(word,tab[mid].word)) < 0)
  69.  
  70.             high = mid - 1;
  71.  
  72.         else if (cond > 0)
  73.  
  74.             low = mid + 1;
  75.  
  76.         else /*found it*/
  77.  
  78.             return mid;
  79.                 }
  80.  
  81.     return -1;
  82.                               }
  83.  
  84. /*linear search*/
  85.  
  86. int search (char* word, struct key tab[], int n) {
  87.  
  88.     int i;
  89.  
  90.     for (i = 0; i < n; ++i) {
  91.  
  92.         if (strcmp(word,tab[i].word) == 0) {
  93.  
  94.  
  95.             return i;
  96.                            }
  97.  
  98.                  }
  99.  
  100.     return -1;
  101.  
  102.                               }
  103.  
  104.  
  105. #define BUFFSIZE 100
  106.  
  107. char BUFF[BUFFSIZE]; /*array for ungetch*/
  108.  
  109. int bufp = 0; /*buffer position, initialized to zero*/
  110.  
  111. int getch (void) { /*get a (possibly pushed-back) character*/
  112.  
  113.     return (bufp > 0) ? BUFF[bufp--] : getchar();
  114.  
  115.  
  116.               }
  117.  
  118.  
  119. void ungetch (int c) { /* push character back on input */
  120.  
  121.     if (bufp > BUFFSIZE) {
  122.    
  123.         printf("ungetch: too many characters\n"); /*if this is filled up*/
  124.  
  125.  
  126.                  }
  127.  
  128.     else {
  129.  
  130.         BUFF[bufp++] = c;   /*if there's still room*/
  131.  
  132.           }
  133.  
  134.               }
  135.  
  136. /*getword: get next word or character from input and read it into word*/
  137.  
  138. int getword (char * word, int lim) {
  139.    
  140.     int c,getch(void);  
  141.  
  142.     void ungetch(int);    
  143.    
  144.     char * w = word;
  145.  
  146.     while (isspace(c = getch())) { 
  147.    
  148.         ;
  149.                       }
  150.     if (!isalpha(c)) {
  151.  
  152.         *w = '\0';
  153.        
  154.         return c;
  155.  
  156.               }
  157.  
  158.     for ( ; --lim > 0; w++) {
  159.        
  160.         if (!isalnum(*w = getch())) {
  161.  
  162.             ungetch(*w);
  163.    
  164.             break;
  165.  
  166.                         }
  167.  
  168.                 }
  169.  
  170.     *w = '\0';
  171.    
  172.     return w[0];
  173.        
  174.                    }
  175.  
  176.  
  177. #define MAXWORD 100
  178.  
  179. /*count C keywords*/
  180.  
  181. int main (int argc, char * argv[]) {
  182.  
  183.     int n;
  184.  
  185.     char word[MAXWORD];
  186.  
  187.     while (getword(word,MAXWORD) != EOF) {
  188.  
  189.         printf("I've got past the beginning of the getword loop.\n");
  190.  
  191.         if(isalpha(word[0])) {
  192.  
  193.         printf("I'm past the word[0] condition.\n");
  194.  
  195.             if ((n = binsearch(word,keytab,NKEYS)) >= 0) { //the problem starts here
  196.  
  197.                 printf("past the search condition.\n");
  198.  
  199.                 keytab[n].count++;
  200.  
  201.                                           }
  202.  
  203.                       }
  204.  
  205.                          }
  206.  
  207.     for (n = 0; n < NKEYS; n++) {
  208.  
  209.         if (keytab[n].count > 0) {
  210.  
  211.             printf("%4d %s\n", keytab[n].count,keytab[n].word);
  212.  
  213.                       }
  214.  
  215.                     }
  216.  
  217.     return -1;
  218.  
  219.                    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement