Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!rochester!rutgers!ll-xn!husc6!cmcl2!brl-adm!adm!franco@MIKEY.BBN.COM From: franco@MIKEY.BBN.COM (Frank A. Lonigro) Newsgroups: comp.unix.wizards Subject: scanf quiz... Message-ID: <8527@brl-adm.ARPA> Date: Tue, 28-Jul-87 12:24:30 EDT Article-I.D.: brl-adm.8527 Posted: Tue Jul 28 12:24:30 1987 Date-Received: Thu, 30-Jul-87 00:46:41 EDT Sender: news@brl-adm.ARPA Lines: 44 In a previous unix-wizards article, lopez wrote that he has an "easy" answer to the SCANF quiz. And it is just that, EASY. Although his solution works, it has a flaw in that the '$' char cannot appear in any part of the batch of strings after the '#' sign. This cuts down on the randomness of strings and restricts the strings to not have a '$' in them. Here is his EASY solution: main() { char s1[100], s2[100], s3[100]; sscanf("one two three four # five six seven", "%s %[^#] \# %[^$]", s1, s2, s3); printf("s1=%s, s2=%s, s3=%s\n", s1, s2, s3); } /*main*/ My solution is similar except I don't use the '$' as a flag for end of string, plus adds to the randomness of strings and doesn't restrict them in any way. main() { char s1[100], s2[100], s3[100]; sscanf("one two three four # five $six seven", "%s %[^#] \# %[\001-\177]", s1, s2, s3); printf("s1=%s, s2=%s, s3=%s\n", s1, s2, s3); } /*main*/ Let me explain: sscanf(msg, "%d %s %[\001-\177]", &x, y, z); ^^^^^^ Here what the format string is saying is to use all ASCII characters but the NULL, which is what you want. -franco%bbn.com@relay.cs.net