TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Kurt Kuzba
from: Jasen Betts
date: 2003-09-21 07:05:50
subject: Snippets ini.c

Hi Kurt.

19-Sep-03 16:17:55, Kurt Kuzba wrote to All


 KK>     Sure.  It's wasteful.  Nobody worries much about that today,

 KK> /*_|_|  notrail.c
 KK> _|_|_|  Public Domain : Kurt Kuzba : Friday, September 19, 2003
 KK> _|_|_|  Simple routine to strip trailing spaces in one pass.
 KK> _|_|_|  No guarantee or warrantee is given or implied.
 KK> _|_|*/
 KK> #include 
 KK> #include 
 KK> #define NUL 0;
 KK> static char *notrail(char *string)

 KK> {
 KK>     int space = 0, iterator = 0;
 KK>     if (string || *string)
 KK>     {
 KK>         for(iterator = space = 0; *(string + iterator); iterator++)
 KK>             if(!isspace(*(string + iterator))) space = iterator;
 KK>     }
 KK>     if(space != iterator) *(string + space + 1) = NUL;
 KK>     return string;
 KK> }

You're teesting string[0] against 0 twice, and zeroing space and iterator
twice)...  (put the last if inside the first if)

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

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

I used pointers more extensively to save arithmetic...

char* notrail(char *string)
{
  if (string)
    {
      char *cursor,*non_space=string-1;  /* pointers save arithmetic */
      for(cursor=string;*cursor;++cursor)
        if (!isspace(*cursor)) non_space=cursor;
      if( non_space+1 != cursor) non_space[1]=0;
    }
  return string;
}

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

 -=> Bye <=-


---
* Origin: As King Arthur said: Some days it all seems so feudal. (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™.