cs1713-day4-prog2.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (C) 2020, 2019, 2018, 2017 Girish M
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  16. * MA 02110-1301, USA.
  17. *
  18. */
  19. /*---------------------------------------------------------------------------
  20. Name: Girish M
  21. Roll number: cs1713
  22. Date: 3 August 2017
  23. Program description: Write a program to count the number of occurrences of
  24. each keyword in a C program.
  25. Acknowledgements:
  26. ---------------------------------------------------------------------------*/
  27. #include "common.h"
  28. int* countKeywordOccurances(const char* keywords[], char* cFile)
  29. {
  30. FILE* fp;
  31. char *line = NULL;
  32. size_t len = 0;
  33. ssize_t read;
  34. char* token;
  35. char* saveptr1;
  36. const char* delim = "{\"\'=*;:()| ";
  37. int i;
  38. static int count[32];
  39. if(NULL == (fp = fopen(cFile,"r")))
  40. {
  41. token = NULL;
  42. printf("\nError opening file\n");
  43. }
  44. else
  45. {
  46. while((read = getline(&line, &len, fp)) != -1)
  47. {
  48. token = strtok_r(line, delim, &saveptr1);
  49. for(i=0; i<32; i++)
  50. {
  51. if(strcmp(token, keywords[i]) == 0)
  52. {
  53. count[i]++;
  54. }
  55. }
  56. }
  57. }
  58. fclose(fp);
  59. return count;
  60. }
  61. int main(int argc, char* argv[])
  62. {
  63. const char* keywordsInC[] = {"auto","break","case","char","const","continue","default","do","double","else","enum","extern","float","for","goto","if","int","long","register","return","short","signed","sizeof","static","struct","switch","typedef","union","unsigned","void","volatile","while"};
  64. if(argc == 2)
  65. {
  66. int i=0, totalKeywords=0;
  67. static int* countOfKeywords;
  68. countOfKeywords = countKeywordOccurances(keywordsInC, argv[1]);
  69. printf("\n**************************************************************************\n");
  70. for(i=0; i<32; i++)
  71. {
  72. if(countOfKeywords[i] > 0)
  73. {
  74. printf("\nNo.of times %s keyword occurs: %d\n", keywordsInC[i], countOfKeywords[i]);
  75. totalKeywords = totalKeywords + countOfKeywords[i];
  76. }
  77. }
  78. printf("\nTotal no. of occurrances of keywords in %s: %d\n", argv[1], totalKeywords);
  79. printf("\n***************************************************************************\n");
  80. }
  81. else
  82. {
  83. printf("\nUsage: ./cs1713-day4-prog2.o cFile\n");
  84. }
  85. return 0;
  86. }