mawia
mawia

Reputation: 9349

Suppressing blankspace in input through scanf

To suppress newline we use %[^\n]. Can you suggest what should be the format to skip blank in the input, i.e. if we have to input "hallo stackflow". I know fgets and gets but i don't want to use them, they are creating problem.

Upvotes: 0

Views: 373

Answers (2)

dirkgently
dirkgently

Reputation: 111130

I think you mean "include whitespace". Use:

#define str(x) #x
#define xstr(x) str(x)
/* ... */
char buf[ SIZE + 1 ] = "";
int rc = scanf("%" xstr(SIZE) "[^\n]%*[^\n]", buf);
/*you may need the return value later on, if reading
  in multiple strings with whitespaces in a loop */
if (!feof(stdin)) 
     getchar(); /* consume newline */

Upvotes: 1

Charlie Martin
Charlie Martin

Reputation: 112366

Put a blank in the scanf format; that suppresses whitespace.

Upvotes: 0

Related Questions