#include<stdio.h> #include<string.h> void keyword(char str[10]) { if(strcmp("for",str)==0||strcmp("while",str)==0||strcmp("do",str)==0||strcmp("int",str)==0 ||strcmp("float",str)==0||strcmp("char",str)==0||strcmp("double",str)==0||strcmp("static",str)==0|| strcmp("switch",str)==0||strcmp("case",str)==0||strcmp("if",str)==0||strcmp("else",str)==0||strcmp("extern",str)==0|| strcmp("register",str)==0||strcmp("auto",str)==0||strcmp("struct",str)==0||strcmp("while",str)==0||strcmp("do",str)==0|| strcmp("volatile",str)==0||strcmp("sizeof",str)==0||strcmp("goto",str)==0||strcmp("default",str)==0||strcmp("void",str)==0|| strcmp("signed",str)==0||strcmp("continue",str)==0||strcmp("unsigned",str)==0||strcmp("short",str)==0||strcmp("const",str)==0|| strcmp("union",str)==0||strcmp("return",str)==0||strcmp("typedef",str)==0||strcmp("enum",str)==0||strcmp("case",str)==0|| strcmp("long",str)==0||strcmp("break",str)==0) printf("\n%s is a keyword",str); } int main() { FILE *fp; fp=fopen("k.txt","r"); if(fp==NULL) { printf("Error !"); return 0; } char word[20],c; int i=0,j=0; while((c=(char)fgetc(fp))!=EOF) { if(c!=' '){ word[j++]=c; } else{ word[j]='\0'; keyword(word); j=0; } } fclose(fp); return 0; }
Input File ( In k.txt file)
hello if this is int else print
Output :-