I am writing a program in c that searches through source code files and counts the number of ‘C’ reserved words encountered. But the reserved word is printed only if the reserved word entered is first word. And it counts total number of strings not total no of reserved word used. Can someone help me on this. My code is so messy please don't mind that.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define KEYMAX 32
FILE *fp ;
char data[1024];
struct keyword
{
char word[10];
int occur;
};
int i = 0, j = 0, pos;
char str[100], unit[20], ch;
int stored[1024];
char delimiters[] = " \t\n\v\f\r"; /* possible space delimiters */
char *token;
struct keyword key[32] = {"auto", 0, "break", 0, "case", 0,
"char", 0, "const", 0, "continue", 0,
"default", 0, "do", 0, "double", 0,
"else", 0, "enum", 0, "extern", 0,
"float", 0, "for", 0, "goto", 0,
"if", 0, "int", 0, "long", 0,
"register", 0, "return", 0, "short", 0,
"signed", 0, "sizeof", 0, "static", 0,
"struct", 0, "switch", 0, "typedef", 0,
"union", 0, "unsigned", 0, "void", 0,
"volatile", 0, "while", 0,};
int main()
{
takeinput();
system("CLS");
theresult();
// processresult();
// ctoken();
return (0);
}
int takeinput() // function to write in the file
{
printf( "**********Welcome*************" ) ;
fp = fopen("test.c", "w") ; // Open file in write mode.
if ( fp == NULL )
{
printf( "Could not open file test.c" ) ; // Prints the statement if the file is not able to open.
return 1;
}
printf( "\nPlease enter some text from keyboard to write in the file test.c \n\t" ) ;
// getting input from user
while ( strlen ( gets( data ) ) > 0 )
{
// writing in the file
fputs(data, fp) ; // Writes to file
fputs("\n", fp) ;
}
// closing the file
fclose(fp) ;
return 0;
}
int theresult()
{
fp = fopen("test.c", "r"); // read mode
if (fp == NULL)
{
perror("Error while opening the file.\n"); // Prints the statement if the file is not able to open.
return 1;
}
printf("The contents of test.c file are:\n");
// To covert the ch into str
int i= 0;
// printf("-----this is from ch----\n"); (Just for reference)
while((ch = fgetc(fp)) != EOF)
{
str[i]=ch;
i++;
// printf("%c",ch); prints character
}
printf("%s",str);
// printf("\n----This is from token-----\n"); (just for reference)
for (token = strtok(str, delimiters); token != NULL;
token = strtok(NULL, delimiters)) /* 'for loop' conditional part */
/* prints token one per line */
// puts(token); // prints token
for (i = 0; i < strlen(str); i++)
{
while (i < strlen(str) && str[i] != ' ' && isalpha(str[i]))
{
unit[j++] = tolower(str[i++]);
}
if (j != 0)
{
unit[j] = '\0';
pos = binarysearch(unit, key);
j = 0;
if (pos != -1)
{
key[pos].occur++;
}
}
}
printf("***********************\n Keyword\tCount\n***********************\n");
for (i = 0; i < KEYMAX; i++)
{
if (key[i].occur)
{
printf(" %s\t %d\n", key[i].word, key[i].occur); // Prints the reserved keyword and its occurance
}
}
fclose(fp);
return (0);
}
int binarysearch(char *word, struct keyword key[])
{
int low, high, mid;
low = 0;
high = KEYMAX - 1;
while (low <= high)
{
mid = (low + high) / 2;
if (strcmp(word, key[mid].word) < 0)
{
high = mid - 1;
}
else if (strcmp(word, key[mid].word) > 0)
{
low = mid + 1;
}
else
{
return mid;
}
}
return -1;
}
The string entered is: if i break please re-join it. float float
Keyword Count
if 1
break 1
Float 2