Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Lex program to check whether given string is Palindrome or Not

  • Last Updated : 01 May, 2019

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.

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

 

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:




My Personal Notes arrow_drop_up
Recommended Articles
Page :