Scan behavior in scanf in C

I tried to do some things with scanning in scanf, but got stuck somewhere.

when I write

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

input : AHJHkiuy
Output: AHJH

and wherein

scanf("%[^\n]s",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 like:

ABCDahaj ahajABCD ajak12347ab

and you want the result to be as follows:

ABCDahaj ahajABCD ajak

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

+6
source share
3 answers

You can expand your example a bit and achieve your goal.

scanf("%[A-Za-z ]", s1);
+5
source

Another way to do this:

scanf("%[^0-9]", s1); /* Scans everything until a digit */
+1
source
  1. : scanf ("% s", str); // scanf ("% [a] s", str); // scanset , "a" "aaa" .. ( ).
  2. : "-" . scanf ("% [0–9] s", .); // scanset , 0 9. .
  3. : (^) . scanf ("% [^ A] s", .); // scanset , , 'A ( ).
  4. scanset: scanf ("% [^\n] s", str); // scanset , ('\n).
  5. Multiline Input Acceptance: This is the use of the third use mentioned above. Suppose you want to get input for a paragraph or group of words. Normal scanf will only scan the first word. But using scanset, we can provide paragraph input and save them as one complete line. We must accept the breakpoint for the paragraph, which in this case is the β€œ+” character, and accept the input. scanf ("% [^ +] s", p.); // Use scanset
0
source

All Articles