12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- /*
- * Copyright (C) 2020, 2019, 2018, 2017 Girish M
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- * MA 02110-1301, USA.
- *
- */
- /*---------------------------------------------------------------------------
- Name: Girish M
- Roll number: cs1713
- Date: 3 August 2017
- Program description: Write a program to count the number of occurrences of
- each keyword in a C program.
- Acknowledgements:
- ---------------------------------------------------------------------------*/
- #include "common.h"
- int* countKeywordOccurances(const char* keywords[], char* cFile)
- {
- FILE* fp;
- char *line = NULL;
- size_t len = 0;
- ssize_t read;
- char* token;
- char* saveptr1;
- const char* delim = "{\"\'=*;:()| ";
- int i;
- static int count[32];
- if(NULL == (fp = fopen(cFile,"r")))
- {
- token = NULL;
- printf("\nError opening file\n");
- }
- else
- {
- while((read = getline(&line, &len, fp)) != -1)
- {
- token = strtok_r(line, delim, &saveptr1);
- for(i=0; i<32; i++)
- {
- if(strcmp(token, keywords[i]) == 0)
- {
- count[i]++;
- }
- }
- }
- }
- fclose(fp);
- return count;
- }
- int main(int argc, char* argv[])
- {
- 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"};
-
- if(argc == 2)
- {
- int i=0, totalKeywords=0;
- static int* countOfKeywords;
- countOfKeywords = countKeywordOccurances(keywordsInC, argv[1]);
- printf("\n**************************************************************************\n");
- for(i=0; i<32; i++)
- {
- if(countOfKeywords[i] > 0)
- {
- printf("\nNo.of times %s keyword occurs: %d\n", keywordsInC[i], countOfKeywords[i]);
- totalKeywords = totalKeywords + countOfKeywords[i];
- }
- }
- printf("\nTotal no. of occurrances of keywords in %s: %d\n", argv[1], totalKeywords);
- printf("\n***************************************************************************\n");
- }
- else
- {
- printf("\nUsage: ./cs1713-day4-prog2.o cFile\n");
- }
- return 0;
- }
|