TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: All
from: Kurt Kuzba
date: 2003-09-23 01:59:12
subject: Snippets ini.c

From: kkuzba{at}centurytel.net
To: c_echo{at}yahoogroups.com

* Author: Jasen Betts
JB>  You're teesting string[0] against 0 twice, and zeroing space
JB>  and iterator twice)...  (put the last if inside the first if)

    The integer variable "space" is incremented only if the char
 value is not a space in the first if.  In the second if, the value
 of the integer value "iterator" is compared to "space" to see if
 there are trailing spaces, which will be indicated by a difference
 in the two values.  If the second if were placed inside the first,
 then it would provide a NUL terminator at the location of "space"
 on each iteration in which a space had been encountered, even if
 there were subsequent nonspace characters remaining in the
 unparsed portion of the string.  This is a one-pass solution.

JB>  also all-space strings are reduced to a single space instead
JB>  of empty. (can be fixed by initilaising iterator to -1 in the
JB>  for loop)

 Had I thought of it, I would have added a line testing the value
 of "space".

    if(!space) *string = NUL;

 This is simple and quick, and "space" will only be equal to zero
 if the only character remaining is a space.  Now that I think of
 it, though, the -1 initialization that you suggested would work
 just fine and provide a more elegant solution, given that "space"
 is not advanced incrementally, but is assigned equality to
 "iterator" upon finding a nonspace char value.  Good one.
 Yes.  It works perfectly!  (just tried it out...)

JB>  and why the *(pointer+integer) syntax instead of
JB>  pointer[integer] ?

 Preservation of arithmetic values, mostly, in a strictly aesthetic
 sense.  The compiler will, I believe, treat p[x] and *(p + x) in
 the same fashion at compile time, resulting in identical code as
 found when the array notation is used instead of pointer notation.

JB>  For long strings determining the length first and then working
JB>  backwards may be more efficient like Bob Stout's code.

    Maybe, but you are stepping through the string and then backing
 up in that case, and I wanted to just go through it one time and
 call it done.  That is why I have the separate variable to recall
 the position of the last non-space char value, and make it the
 final character in the string.

>  kkzuba{at}centurytel.net   http://home.centurytel.net/kkuzba
>  'We have been warned against Fangorn. But one so knowing
>  will not have forgotten that.'

--- SoupGate-Win32 v1.05
* Origin: kkuzba{at}centurytel.net (2:292/516.666)
SEEN-BY: 633/267 270
@PATH: 292/516 854 140/1 106/2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.