Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Lex program to find the Length of a String

  • Last Updated : 30 Apr, 2019

Problem: Write a Lex program to find the Length of a String

Explanation:
FLEX (Fast Lexical Analyzer Generator) 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.

We provide nothing but the best curated videos and practice problems for our students. Check out the C Foundation Course and master the C language from basic to advanced level. Wait no more, start learning today!

Examples:

Input: geeksforgeeks
Output: length of given string is : 13

Input: geeks
Output: length of given string is : 5 

Implementation:




/*lex program to find the length of a string*/
  
%{
  #include<stdio.h> 
  int  length;
%}
  
/* Rules Section*/
%%
  [a-z A-Z 0-9]+ {length=yyleng; }
%%
  
int main()
 {
  yylex();
  printf("length of given string is : %d", length);
  return 0;
 }

Output:

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!