Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compare strings using sscanf, but ignore whitespaces

Tags:

c

scanf

For a command line application I need to compare the input string to a command pattern. White spaces need to be ignored.

This line should match input strings like " drop all " and " drop all":

int rc = sscanf( input, "drop all");

But what does indicate a successful match here?

like image 493
mwarning Avatar asked Jan 30 '23 00:01

mwarning


1 Answers

Use "%n" to record where the scanning stopped.

Add white space in the format to wherever WS in input needs to be ignored.

int n = -1;
sscanf( input, " drop all %n", &n);
//  v---- Did scanning reach the end of the format? 
//  |         v---- Was there additional text in `input`? 
if (n >= 0 && input[n] == '\0') Success();
like image 144
chux - Reinstate Monica Avatar answered Feb 12 '23 09:02

chux - Reinstate Monica