Sunday 24 April 2016

Lex program to find the length of the longest word

 /*Write a Lex program that finds the length of the longest word (defined as a contiguous
 string of upper and lower case letters) in the input.*/
 
%{ #include<stdio.h>
int k=0;
%}

%%
[a-zA-Z]+ {
if(yyleng>k)
{  k= yyleng;
}
}
%%

int main(int argc[],char **argv[])
{
 yyin=fopen("abc.txt","r");
 yylex(); 
 printf("largest: %d",k);
 printf("\n");
 return 0;
}
 
 //file abc.txt
 
Lex program to find the length of the longest word
Lex program to find the length of the longest word

//Output of the above program

Lex program to find the length of the longest word

Lex program to find the length of the longest word

Checkout this video on Lex Program to Find the Length of the longest word.


2 comments: