home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 154_01 / mark.c < prev    next >
Text File  |  1979-12-31  |  2KB  |  81 lines

  1. /* mark.c:    underline C reserved words from standard input */
  2.  
  3. #include <stdio.h>
  4. #include <ctype.h>
  5.  
  6. char line[81], nextline[81];
  7. int pos;
  8.  
  9. main()
  10. {
  11.     int start, i;
  12.     char word[21];
  13.  
  14.     while (gets(line) != NULL) {
  15.         /* ..clear nextline.. */
  16.         reset();
  17.  
  18.         /* ..process each line in turn.. */
  19.         while ((start = getword(word)) >= 0)
  20.             if (search(word))
  21.                 /* .. a reserved word was found - mark nextline.. */
  22.                 for (i = start; i < start+strlen(word); ++i)
  23.                     nextline[i] = '_';
  24.                 nextline[strlen(line)+1]= '\0';
  25.  
  26.         /* ..print line and underscores, if any.. */
  27.         printf("%s\r%s\n",line,nextline);
  28.     }
  29. }
  30.  
  31. reset()            /* ..clear nextline - start at beginning of line.. */
  32. {
  33.     int i;
  34.     
  35.     for (i = 0; i < 80; ++i)
  36.         nextline[i] = ' ';
  37.     pos = 0;
  38. }
  39.  
  40. int search(s)    /* ..see if s is a reserved word.. */
  41. char *s;
  42. {
  43.     static char *reserved[] = { "auto", "break", "case", "char",
  44.         "continue", "default", "do", "double", "else", "extern",
  45.         "float", "for", "goto", "if", "int", "long", "register",
  46.         "return", "short", "sizeof", "static", "struct", "switch",
  47.         "typedef", "union", "unsigned", "while" };
  48.     int i, flag;
  49.  
  50.     for (i = 0; i < 27 && (flag = strcmp(reserved[i],s)) <= 0; ++i)
  51.         if (flag == 0)
  52.             /* ..s is a reserved word.. */
  53.             return 1;
  54.  
  55.     return 0;
  56. }
  57.  
  58. int getword(s)
  59. char *s;
  60. {
  61.     int c, start, p;
  62.  
  63.     for (c = pos; line[c] != '\0' && !islower(line[c]); ++c) ;
  64.  
  65.     if (line[c] == '\0')
  66.          /* ..no more words on line.. */
  67.         return EOF;
  68.  
  69.     /* ..build word.. */
  70.     start = c, p = 0;
  71.     while (islower(line[c]))
  72.         s[p++] = line[c++];
  73.     s[p] = '\0';
  74.  
  75.     /* ..save position in line.. */
  76.     pos = c;
  77.  
  78.     /* ..return start position of line.. */
  79.     return start;
  80. }
  81.