| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Snippets ini.c |
Hi Kurt.
23-Sep-03 01:59:13, Kurt Kuzba wrote to All
KK> From: kkuzba{at}centurytel.net To: c_echo{at}yahoogroups.com
KK> * Author: Jasen Betts
JB>> You're testing string[0] against 0 twice, and zeroing space and
JB>> iterator twice)... (put the last if inside the first if)
KK> The integer variable "space" is incremented only if the char value
KK> is not a space in the first if. In the second if, the value of
KK> the integer value "iterator" is compared to
"space" to see if
KK> there are trailing spaces, which will be indicated by a difference
KK> in the two values. If the second if were placed inside the first,
KK> then it would provide a NUL terminator at the location of "space"
KK> on each iteration in which a space had been encountered, even if
KK> there were subsequent nonspace characters remaining in the
KK> unparsed portion of the string. This is a one-pass solution.
I meant do this:
static char *notrail(char *string)
{
if (string)
{
int space = -1, iterator = 0;
for(; *(string + iterator); iterator++)
if(!isspace(*(string + iterator))) space = iterator;
if( ++space != iterator) *(string + space) = NUL;
}
return string;
}
KK> Had I thought of it, I would have added a line testing the value
KK> of "space".
KK> if(!space) *string = NUL;
KK> This is simple and quick, and "space" will only be equal to zero
KK> if the only character remaining is a space.
are you sure?
that would'nt work with things like "a" and "b "
JB>> For long strings determining the length first and then working
JB>> backwards may be more efficient like Bob Stout's code.
KK> Maybe, but you are stepping through the string and then backing up
KK> in that case, and I wanted to just go through it one time and call
KK> it done.
yeah, that was my first take on the problem too. but in many cases each
isspace() is extra an array lookup (check ctype.h to see how isspace is
implemented by your compiler(s))
If you're trimming less than half of the string Bob's backwards-seek is
more efficient, and the code which uses strlen() is in many cases already
using optimised assembler to do most of the work.
-=> Bye <=-
---
* Origin: Black Holes were created when God divided by zero! (3:640/1042)SEEN-BY: 633/267 270 @PATH: 640/1042 531 954 774/605 123/500 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™.