how to check no. of spaces were skipped?

N

nick

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!
 
R

Richard Bos

nick said:
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.
 
S

sonic0568

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...
 
E

Eric Sosman

nick said:
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.
 
R

Robert Gamble

nick said:
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
 
N

nick

Robert said:
nick said:
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
 
K

Keith Thompson

sonic0568 said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top