Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how to check no. of spaces were skipped?

Reply
Thread Tools

how to check no. of spaces were skipped?

 
 
Johny
Guest
Posts: n/a
 
      12-08-2005
don't use scanf()

 
Reply With Quote
 
 
 
 
nick
Guest
Posts: n/a
 
      12-08-2005
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!
 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      12-08-2005
nick <(E-Mail Removed)> 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.
 
Reply With Quote
 
sonic0568
Guest
Posts: n/a
 
      12-08-2005
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...


 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      12-08-2005
On 2005-12-08, sonic0568 <(E-Mail Removed)> 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...

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      12-08-2005
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
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Robert Gamble
Guest
Posts: n/a
 
      12-08-2005
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

 
Reply With Quote
 
nick
Guest
Posts: n/a
 
      12-08-2005
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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-08-2005
"sonic0568" <(E-Mail Removed)> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
[CSS] how can I show spaces as spaces? Tomasz Chmielewski HTML 21 09-10-2009 06:43 PM
how can I show spaces as spaces, part 2 Tomasz Chmielewski HTML 14 09-10-2009 03:54 PM
Re: How to trim a String trailing spaces, but not leading spaces? Roedy Green Java 3 09-14-2008 02:10 AM
Re: How to trim a String trailing spaces, but not leading spaces? John B. Matthews Java 4 09-12-2008 05:28 AM



Advertisments