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.
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:

Recommended Posts:
- YACC program to check whether given string is Palindrome or not
- Lex program to check whether a given number is even or odd
- Lex program to check if a Date is valid or not
- C program to check syntax of 'for' loop
- Lex Program to check whether a number is Prime or Not
- Lex program to check whether the input is digit or not
- Lex program to check whether input number is odd or even
- Lex program to check perfect numbers
- Lex Program to check valid email
- Lex program to check valid Mobile Number
- Lex program to find the Length of a String
- Lex Program to accept string starting with vowel
- YACC program to recognize string with grammar { anbn | n≥0 }
- LEX program to count the number of vowels and consonants in a given string
- Lex program to check whether given number is armstrong number or not
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.

