C Program to Count the Occurrences of Each Keyword using Array Structure

«
»
This C Program counts the occurrences of each C keyword using array structure. C keywords are the words which have a definition and cannot be used as an identifier.

Here is a source code of the C program to count the occurrences of each C keyword using array structure. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Count the Occurrences of each C Keyword
  3.  * using Array Structure
  4.  */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8. #define KEYMAX 32
  9.  
  10. struct keyword
  11. {
  12.     char word[10];
  13.     int occur;
  14. };
  15.  
  16. int binarysearch(char [], struct keyword[]);
  17.  
  18. int main()
  19. {
  20.     int i = 0, j = 0, pos;
  21.     char string[100], unit[20], c;
  22.     struct keyword key[32] = {"auto", 0, "break", 0, "case", 0,
  23.                           "char", 0, "const", 0, "continue", 0,
  24.                           "default", 0, "do", 0, "double", 0,
  25.                           "else", 0, "enum", 0, "extern", 0,
  26.                           "float", 0, "for", 0, "goto", 0,
  27.                           "if", 0, "int", 0, "long", 0,
  28.                           "register", 0, "return", 0, "short", 0,
  29.                           "signed", 0, "sizeof", 0, "static", 0,
  30.                           "struct", 0, "switch", 0, "typedef", 0,
  31.                           "union", 0, "unsigned", 0, "void", 0,
  32.                           "volatile", 0, "while", 0,};
  33.  
  34.     printf("Enter string: ");
  35.     do
  36.     {
  37.         fflush(stdin);
  38.         c = getchar();
  39.         string[i++] = c;
  40.  
  41.     } while (c != '\n');
  42.     string[i - 1] = '\0';
  43.     printf("The string entered is: %s\n", string);
  44.     for (i = 0; i < strlen(string); i++)
  45.     {
  46.         while (i < strlen(string) && string[i] != ' ' && isalpha(string[i]))
  47.         {
  48.             unit[j++] = tolower(string[i++]);
  49.         }
  50.         if (j != 0)
  51.         {
  52.             unit[j] = '\0';
  53.             pos = binarysearch(unit, key);
  54.             j = 0;
  55.             if (pos != -1)
  56.             {
  57.                key[pos].occur++;
  58.             }
  59.         }
  60.     }
  61.     printf("***********************\n   Keyword\tCount\n***********************\n");
  62.     for (i = 0; i < KEYMAX; i++)
  63.     {
  64.         if (key[i].occur)
  65.         {
  66.             printf("    %s\t  %d\n", key[i].word, key[i].occur);
  67.         }
  68.     }
  69.  
  70.     return 0;
  71. }
  72.  
  73. int binarysearch(char *word, struct keyword key[])
  74. {
  75.     int low, high, mid;
  76.  
  77.     low = 0;
  78.     high = KEYMAX - 1;
  79.     while (low <= high)
  80.     {
  81.         mid = (low + high) / 2;
  82.         if (strcmp(word, key[mid].word) < 0)
  83.         {
  84.             high = mid - 1;
  85.         }
  86.         else if (strcmp(word, key[mid].word) > 0)
  87.         {
  88.             low = mid + 1;
  89.         }
  90.         else
  91.         {
  92.             return mid;
  93.         }
  94.     }
  95.  
  96.     return -1;
  97. }

$ gcc keywordoccur.c 
$ ./a.out
Enter string: break, float and double are c keywords. float and double are primitive data types.  
The string entered is: break, float and double are c keywords. float and double are primitive data types.
***********************
   Keyword	Count
***********************
    break	  1
    double	  2
    float	  2

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.