Lex program to check whether given string is Palindrome or Not

Problem: Write a Lex program to check whether given string is Palindrome or Not.

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.


Input: Enter a string : naman 
Output: Given string is Palindrome

Input: Enter a string : geeksforgeeks
Output: Given string is not Palindrome 






/* 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;
    if (flag == 1)
        printf("Given string is Palindrome");
        printf("Given string is not Palindrome");
% %
    // driver code
    int main()
    printf("Enter a string :");
    return 0;
int yywrap()
    return 1;



GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.