How to use scanf to read Strings with Spaces ?

I knew this for two years now, but it dint strike me before to put it up in the blog; don’t know why ?

Its easy and simple, I don’t think I need to explain. Only the syntax needs to be remembered.

scanf(” %[^\n]s”,a);

May be you get a question in your mind. What does ‘[^\n]‘ do ? Does it set the delimiter to ‘\n’. Well, you on the right track. Yes, it does. Instead on ‘\n’, if you had put a ‘\t’ it would consider all words with spaces as string until you press a ‘Tab’. You can even have a  ‘  ‘(a space). The scanf can be expanded as

scanf(” %[^ ]s”,a);  // Note – There is a space after ^

The only differece between scanf(” %s”,a) and scanf(” %[^ ]s”,a) is that, when you enter a string with two words, the former considers each word as a new string whereas the latter consider only the first word as a string and the other word is ignored.

As a Example, consider the string “Hello World“, the former reads “Hello” and “World” as two strings (if you had called ‘scanf’ twice) and the latter reads only the first word “Hello” (even if you had called ‘scanf’ twice) ! Go ahead and experiment with other delimiters !! :D

About these ads

15 Comments »

  1. Wow boss thanks.

    It is so interesting.

    Thank You

  2. 2
    Apar Singhal Says:

    thnx.. but what u wrote in last two paragraphs is not working. In both the cases scanf(“%s”, str); & scanf(“%[^ ]s”, str); it is taking the two words as two strings.

  3. 3
    Bert Says:

    Use either %[] or %s, but not %[]s

  4. 4
    Bert Says:

    The reason that in the example of the author only the first word is matched, is that the ‘s’ after the specifier (which is %[^ ]) is never matched! Furthermore, %s is the same as %[^ \t\r\n]

    • 5
      ray040123 Says:

      You are right! The author’s misleading something. One shound not add ‘s’ after %[].

  5. 6
    Kanishka Says:

    This is fantastic ! You saved my day ! Thanks a lot !

  6. 7
    Tedy Says:

    Thanks dude, you kinda saved ma day. It worked like a charm. Keep up the good work!

  7. 8
    santiago Says:

    Great, it is explained in the standard for C99, but the explanation there is quite obtrusive…

    Apparently, you also could use the characters you allow in the string just removing the ^, and you can use – for an interval of letters, for instance:

    char str[256];
    scanf(” %[ A-Z]255s”, str);

    will read an string that includes only blank spaces and capital letters up to 255 chars.

  8. 9
    Homitek Says:

    Thanks a lot man! Not exactly what I was looking for, but it solved my problem nicely.

  9. 10
    Mustafa Hassan Says:

    Thankyou.. it is very helpful :)

    • 11
      Aniruddha Says:

      actually what I could find is – %[ is really a nice and a weired format specifier, we can say.
      cuz, scanf("%[^\n]s”, str) will match all the words(characters) that are not on newline…so each time a multiword string will be accepted by this function over here.
      whereas, scanf(“%[^ ]s”, str) with space after caret (^) will not take multiword string as input since it has got the limitation of spaces..so it won’t match the characters with through spaces, Am I right?

  10. 12
    regelink Says:

    as i learned tht we need to use “&” in scanf to read values..bt the func u used above doesn’t satisfy this…?can any1 explain me y this so?

    • 13
      mike Says:

      An array is a pointer, and does not need to be de-referenced. (if i remember correctly, sorry, it’s 2am, and gets() is pissing me off right now.)
      you know what they say with gets(): Nothing, because they had a whitespace before the string.

  11. 14
    shahbaaz Says:

    How to make scanf read strings with many space. e.i if i want to enter a name “Shah Rukh Khan” ?

  12. 15
    Shahbaaz Says:

    How to use this funtion twice in a program??


RSS Feed for this entry

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 1,545 other followers

%d bloggers like this: