how to check no. of spaces were skipped?

Discussion in 'C Programming' started by Johny, Dec 8, 2005.

  1. Johny

    Johny Guest

    don't use scanf()
     
    Johny, Dec 8, 2005
    #1
    1. Advertising

  2. Johny

    nick Guest

    i use sscanf()to get the words in a line, it will skip all the space
    automatically, if i want to know how many spaces were skipped and get
    the words in a line, what can i do?
    thanks!
     
    nick, Dec 8, 2005
    #2
    1. Advertising

  3. Johny

    Richard Bos Guest

    nick <> wrote:

    > i use sscanf()to get the words in a line, it will skip all the space
    > automatically, if i want to know how many spaces were skipped and get
    > the words in a line, what can i do?
    > thanks!


    Don't use sscanf()[1]. Use strcspn() instead.

    Richard

    [1] In this case. Out of *scanf(), it is the most useful generally.
     
    Richard Bos, Dec 8, 2005
    #3
  4. Johny

    sonic0568 Guest

    what's the difference between rand() & random() ?

    thanx!
    i am the new comer here,maybe i break some rules that i don't kown
    before;please forgive me...
     
    sonic0568, Dec 8, 2005
    #4
  5. Johny

    Jordan Abel Guest

    On 2005-12-08, sonic0568 <> wrote:
    > what's the difference between rand() & random() ?


    The latter is not part of the C standard, and evidently originated with
    4.2 BSD.

    > thanx! i am the new comer here,maybe i break some rules that i don't
    > kown before; please forgive me...
     
    Jordan Abel, Dec 8, 2005
    #5
  6. Johny

    Eric Sosman Guest

    nick wrote:
    > i use sscanf()to get the words in a line, it will skip all the space
    > automatically, if i want to know how many spaces were skipped and get
    > the words in a line, what can i do?


    sscanf() is not the best tool for the job, as others
    have mentioned. But if you have some kind of passionate
    desire to use sscanf(), look up the "%n" specifier.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Dec 8, 2005
    #6
  7. nick wrote:
    > i use sscanf()to get the words in a line, it will skip all the space
    > automatically, if i want to know how many spaces were skipped and get
    > the words in a line, what can i do?
    > thanks!


    You can modify your format string to explicitly skip the whitespace
    first, then use the %n conversion specifier to determine how much space
    was skipped. The %n conversion stored the number of characters
    consumed so far into the specified int. For example, if your original
    call looked like this (buffer size and error checking removed for
    clarity):

    char buf[100];
    scanf("%s", buf);

    which would skip leading whitespace and put the first word into buf,
    you could change it to:

    char buf[100];
    int whitespace;
    scanf(" %n%s", &whitespace, buf);

    and the number of whitespace characters skipped would be stored into
    "whitespace".

    If you only want to skip space characters or some other specific set of
    characters, you can also use this technique with an
    assignment-suppressing character set specifier:

    char buf[100];
    int skipped;
    scanf("%*[ _]%n%s", &skipped, buf);

    The above example will skip all leading space and underscore
    characters, store the number of skipped characters into "skipped", and
    read the next word into buf. Note that the %s conversion specifier may
    skip additional whitespace characters besides the space character so
    this example would not catch those characters or space/underscore
    characters following them.

    You can also use this technique in the middle of a large format string
    as well, although it requires double the effort and some subtraction:

    scanf("...%s%n %n%s...", ... buf1, val1, val2, buf2 ...);
    The amount of whitespace skipped before the second %s conversion is
    then val2-val1.

    Robert Gamble
     
    Robert Gamble, Dec 8, 2005
    #7
  8. Johny

    nick Guest

    Robert Gamble wrote:
    > nick wrote:
    >
    >>i use sscanf()to get the words in a line, it will skip all the space
    >>automatically, if i want to know how many spaces were skipped and get
    >>the words in a line, what can i do?
    >>thanks!

    >
    >
    > You can modify your format string to explicitly skip the whitespace
    > first, then use the %n conversion specifier to determine how much space
    > was skipped. The %n conversion stored the number of characters
    > consumed so far into the specified int. For example, if your original
    > call looked like this (buffer size and error checking removed for
    > clarity):
    >
    > char buf[100];
    > scanf("%s", buf);
    >
    > which would skip leading whitespace and put the first word into buf,
    > you could change it to:
    >
    > char buf[100];
    > int whitespace;
    > scanf(" %n%s", &whitespace, buf);
    >
    > and the number of whitespace characters skipped would be stored into
    > "whitespace".
    >
    > If you only want to skip space characters or some other specific set of
    > characters, you can also use this technique with an
    > assignment-suppressing character set specifier:
    >
    > char buf[100];
    > int skipped;
    > scanf("%*[ _]%n%s", &skipped, buf);
    >
    > The above example will skip all leading space and underscore
    > characters, store the number of skipped characters into "skipped", and
    > read the next word into buf. Note that the %s conversion specifier may
    > skip additional whitespace characters besides the space character so
    > this example would not catch those characters or space/underscore
    > characters following them.
    >
    > You can also use this technique in the middle of a large format string
    > as well, although it requires double the effort and some subtraction:
    >
    > scanf("...%s%n %n%s...", ... buf1, val1, val2, buf2 ...);
    > The amount of whitespace skipped before the second %s conversion is
    > then val2-val1.
    >
    > Robert Gamble
    >

    thanks
     
    nick, Dec 8, 2005
    #8
  9. "sonic0568" <> writes:
    > what's the difference between rand() & random() ?


    Jordan already answered that.

    > i am the new comer here,maybe i break some rules that i don't kown
    > before;please forgive me...


    A new question should start a new thread rather than being a followup
    to an existing one.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Dec 8, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Franz

    Page_Load is skipped

    Franz, Mar 5, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    388
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Mar 5, 2006
  2. Remy Blank
    Replies:
    8
    Views:
    368
    Jeremy Fincher
    Sep 24, 2004
  3. Remy Blank
    Replies:
    0
    Views:
    468
    Remy Blank
    Sep 24, 2004
  4. Bill

    Code being skipped

    Bill, Jan 17, 2007, in forum: C Programming
    Replies:
    6
    Views:
    326
  5. Replies:
    2
    Views:
    245
Loading...

Share This Page