user9815591
user9815591

Reputation: 3

Print the matched string except the last character in lex

I'm quite confused how to go about the following problem. Suppose I match a string in the form 'xyz!' in lex, but now I want to print only the string 'xyz' which doesn't include the last character of the initial matched string. I do know how to print the matched string,

printf("String:%s", yytext)

but not sure how to print only the string 'xyz'. Can someone please clarify this issue. Thanks in advance!

Upvotes: 0

Views: 448

Answers (1)

rici
rici

Reputation: 241721

printf("String:%.*s", yyleng - 1, yytext);

* in a printf format usually means "get the numeric value from the next argument, which must be an int. So if yyleng (which is the token length) were 4, then printf would do the equivalent of printf("String:%.3s", yytext), which would mean "Print at most 3 characters from yytext.

See man printf for lots more details.

Upvotes: 1

Related Questions