TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: All
from: Kurt Kuzba
date: 2003-09-19 16:17:54
subject: Snippets ini.c

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

* Author: Rob Swindell

RS>  So this function (StripTrailingSpaces) calls strlen() twice
RS>  for each trailing space character in the string, and for those
RS>  that don't know, strlen() has to walk through the string
RS>  character by character from the beginning looking for the
RS>  trailing nul-terminator. This is just a silly waste of CPU
RS>  cycles and time.

    Sure.  It's wasteful.  Nobody worries much about that today,
 but if you were writing for a 2MHz embedded system for a low tech
 device meant to be low maintenance and long life, then it would
 matter a bit more.  I was working on a machine with an embedded
 system and a bad connector.  Due to processing time, the error
 caused by vibration which broke a circuit and emulated an
 activated control button didn't show for one full second after the
 offending circuit was manipulated.  That made troubleshooting the
 device difficult.  It took me two months of operating the machine
 to identify and fix the problem.  The only thing that made me feel
 better about it was that nobody else was even able to come up with
 a reasonable idea of what was wrong in the first place, having
 replaced the control button already, which was assumed defective,
 and then having just decided to live with the problem.  GaK!

    If you just want to run through the string once, then just
 run through the string once.

/*_|_|  notrail.c
_|_|_|  Public Domain : Kurt Kuzba : Friday, September 19, 2003
_|_|_|  Simple routine to strip trailing spaces in one pass.
_|_|_|  No guarantee or warrantee is given or implied.
_|_|*/
#include 
#include 
#define NUL 0;
static char *notrail(char *string)
{
    int space = 0, iterator = 0;
    if (string || *string)
    {
        for(iterator = space = 0; *(string + iterator); iterator++)
            if(!isspace(*(string + iterator))) space = iterator;
    }
    if(space != iterator) *(string + space + 1) = NUL;
    return string;
}
int main (void)
{
    char buf[32],
        *test[10] = {"trailing   test  ", "test",
"trailing * ",
        "foo0", "foo1 ", "foo2  ", "foo3
  ", "foo4    ",
        "penultimate", "six spaces      "};
    int iterator;
    for(iterator = 0; iterator < 10; iterator++)
    {
        sprintf(buf, "%s", test[iterator]);
        notrail(buf);
        printf("*%s*\n", buf);
    }
    fgets(buf, 16, stdin);
    return 0;
}
/*_|_|  end notrail.c
_|_|*/

>  kkuzba{at}centurytel.net   http://home.centurytel.net/kkuzba
>  'I doubt if you will find a path quicker than
>  the one that the Orcs chose,' said Aragorn

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