Lex program to search for a word in a file
Lex program to search a word in a file - This program will search a word in the file input.txt and display the message FOUND or NOT FOUND
*************************************
%{
#include<string.h>
void search(char *);
%}
%option noyywrap
/* Rule Section */
%%
[a-zA-Z]+ search(yytext);
%%
int main()
{
// The function that starts the analysis
yylex();
return 0;
}
void search(char *str)
{/* fp as pointer
of File type */
FILE *fp;
char temp[30];
/* fp points to the file input.txt
and opens it in read mode */
fp=fopen("input.txt", "r");
while((fscanf(fp, "%s", temp))!=EOF)
{
if(!(strcmp(temp, str)))
{
printf("FOUND\n");
return;
}
}
printf("NOT FOUND\n");
return;
}
Comments
Post a Comment