By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

how to check no. of spaces were skipped?

P: n/a
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!
Dec 8 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
don't use scanf()

Dec 8 '05 #2

P: n/a
nick <i1********@yahoo.com> 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.
Dec 8 '05 #3

P: n/a
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...
Dec 8 '05 #4

P: n/a
On 2005-12-08, sonic0568 <so*******@sina.com> 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...

Dec 8 '05 #5

P: n/a
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
es*****@acm-dot-org.invalid
Dec 8 '05 #6

P: n/a
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

Dec 8 '05 #7

P: n/a
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
Dec 8 '05 #8

P: n/a
"sonic0568" <so*******@sina.com> 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) ks***@mib.org <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.
Dec 8 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.