Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scanset behaviour in scanf in C

Tags:

c

scanf

I tried to do some stuff with scanset in scanf but stuck somewhere.

when I write

char s1[250];
scanf("%[A-Z]",s1);

input : AHJHkiuy
Output: AHJH

and with this,

scanf("%[^\n]",s1);

input: abcd ABCD hie
output: abcd ABCD hie       /*that is reading white space also (till \n) */

Now My question is, if I give input as:

ABCDahaj ahajABCD ajak12347ab

and want the output as:

ABCDahaj ahajABCD ajak

then how should the format string be written? That is, how should this scanset be used?

like image 239
user1413523 Avatar asked Jul 24 '12 13:07

user1413523


2 Answers

You could expand your example a little bit and achieve your goal.

scanf("%[A-Za-z ]", s1);
like image 150
timrau Avatar answered Oct 11 '22 22:10

timrau


Another way to do this would be:

scanf("%[^0-9]", s1); /* Scans everything until a digit */
like image 33
Spikatrix Avatar answered Oct 11 '22 22:10

Spikatrix