Tuesday 1 April 2014

3. Lex program to search a word in a file

     To search a word, I have stored words in file named "new" and used lex to receive words from keyboard. On receiving each word, I check it with the words in the file. If the word is found, the program prints "FOUND" else "NOT FOUND".

Here is the code :

%{
#include<string.h>
void check(char *);
%}
%%
[a-zA-Z]+ check(yytext);
%%
main()
{
    yylex();
}
void check(char *str)
{
    FILE *fp;
    char temp[30];
    fp=fopen("new","r");
    while((fscanf(fp,"%s",temp))!=EOF)
    {
        if(!(strcmp(temp,str)))
        {
            printf("FOUND\n");
            return;
        }
       
    }
    printf("NOT FOUND\n");
        return;
}

No comments:

Post a Comment