Lex Program to Find if a Character Apart from Alphabet Occurs in a String
Lex is a computer program that generates lexical analyzers. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language.
The commands for executing the lex program are:
lex abc.l (abc is the file name) cc lex.yy.c ./a.out
Problem: Write a lex program to find if a character apart from the alphabet occurs in a given string.
Input : GeeksforGeeks is best Output : other characters are also present Explanation: Because ' ' space is also a character Input : geeksforgeeks Output : only alphabets present Explanation: Only english alphabets are present in the string
- Using the flag to check if other characters are also present or not;
- If “\n” is encountered we will assume that the user has given the string completely and now he wants the result to be displayed
- So according to the state of flag, we will show the output.
- Else if any other character except (a-z, A-Z) is encountered we will make the flag = 1 which we are checking via Regular expression.
Below is the implementation:
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.