Problem: Write a Lex program to check whether given string is Palindrome or Not.
Explanation:
Lex is a computer program that generates lexical analyzers and was written by Mike Lesk and Eric Schmidt. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language.
Description: A string is said to be palindrome if reverse of the string is same as string. For example, “abba” is palindrome, but “abbc” is not palindrome.
Examples:
Input: Enter a string : naman Output: Given string is Palindrome Input: Enter a string : geeksforgeeks Output: Given string is not Palindrome
Implementation:
/* Lex program to check whether - given string is Palindrome or Not */
% {
int i, j, flag;
%
}
/* Rule Section */ % % [a - z A - z 0 - 9]*
{ for (i = 0, j = yyleng - 1; i <= j; i++, j--) {
if (yytext[i] == yytext[j]) {
flag = 1;
}
else {
flag = 0;
break ;
}
}
if (flag == 1)
printf ( "Given string is Palindrome" );
else
printf ( "Given string is not Palindrome" );
} % % // driver code
int main()
{ printf ( "Enter a string :" );
yylex();
return 0;
} int yywrap()
{ return 1;
} |
Output:
Article Tags :