Code:
#include <stdio.h>
#include <string.h>
int main(){
char string[1500];
char word[100];
int i,x,y,keyword=0,operator=0,s=0;
char *result;
char delims[] = " \n";
char *keywords[] = {"auto", "break", "else", "case", "char", "const", "continue", "default", "do", "double", "enum","extern","float","for","goto","if","int","long","register","return","short","signed",
"sizeof","static","struct","switch","typedef","union","unsigned","void","volatile","while",
"_Bool","_Complex","_Imaginary","inline","restrict","NULL"};
char *operators[]={"+", "-", "*", "/", "%","=","+=", "-=", "*=", "/=", "%=", "&=", "|=", "^=", "<<=", ">>=",
"~", "&", "|", "^" ,"<<", ">>" ,"!", "&&", "||","?:", "==", "!=","( )","++", "--",".", "->",
"<", "<=", ">", ">=", "*", "[ ]",",","(typename)","NULL"};
printf("Enter Paragraph which can include spaces and Any number of lines.\n");
printf("To terminate press TAB and Enter.\n");
scanf("%[^\t]",string);
//Count the number of lines
for(i=0;string[i]!='\0';++i) {
if (string[i]=='\n') ++s; }
printf("\nNo. of lines: %d\n",s);
//Tokenies paragraph
strcpy(word, result);
result = strtok( string, delims );
while( result != NULL ) {
strcpy(word, result);
//Count number of keywords
for( x = 0; keywords[ x ] != "NULL"; x++ ){
if ( strcmp ( word, keywords[x]) == 0 )
keyword++;
}
//Count number of operators
for( y = 0; operators[ y ] != "NULL"; y++ ){
if ( strcmp ( word, operators[y]) == 0 )
operator++;
}
result = strtok( NULL, delims );
}
printf( "No. of keywords: %d.\n", keyword );
printf( "No. of operators: %d.\n", operator );
}