| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | printf() |
FA> char *trimchar(char *s,char ch)
FA> {
FA> static char *p;
FA> char* p1;
FA> p = (char*)s;
FA> while(NULL != (p1 = strchr(p,ch)))
FA> {
FA> memmove(p1,(p1+1),strlen(p1)+1);
FA> p = p1;
FA> }
FA> return s;
FA> }
FA> /* Let's go nuts and write out "Word".
FA> Kurt would be proud of this one ;-) */
FA> printf("%s\n",ltrim(ltrimchar(trimchar(ltrimchar(ltrimchar
FA> (ltrim(" Hello World"),'H'),'e'),'l'),'o')) );
A small tear of joy has formed in the corner of my eye! ;)
I am deeply moved by your deeply nested example.
I might suggest a minor alteration as a speed optimization:
char *del_chr(char *s, int ch)
{
char *spS;
int iLen;
if(s && *s)
{
iLen = strlen(s) + 1;
while(NULL != (spS = strchr(s, ch)))
memmove(spS, (spS + 1), iLen - (spS - s));
}
return s;
}
Removing mathematical calculations in the code where a
variable already containing the value being calculated may
be used will lessen the resource requirements, so I made a
variable to hold strlen(s) + 1. Now we may be moving too
much data, however, so we can use pointer math to arrive at
the correct number of bytes to be moved, subtracting the
difference of our strchr() pointer and our string pointer.
The removal of the function call from the iterative loop
should make it a bit faster on longer strings in which many
instances of the character are to be removed.
I might also supply a function which dupes the string,
leaving out the unwanted characters along the way.
This duplication method may not be as fast as library
routines in most compilers, but compiler optimizations and
improvements in hardware in more modern environments would
make the differences minimal. They make the unnecessary
calculation and function call in your original code pretty
meaningless, too, naturally, but I felt as though this post
required an impassioned response, my having been provoked,
in my twisted little mind, beyond all reason.
char *del_chr_cpy(char *s, int ch)
{
char *pS = s;
int iLen, iS, ipS;
if(s && *s)
{
iLen = strlen(s) + 1;
pS = malloc(iLen);
if(pS)
for(iS = ipS = 0; iS < iLen; iS++)
if(s[iS] != ch)
pS[ipS++] = s[iS];
}
return pS;
}
> ] * Origin: *YOPS ]I[* 8.4 GIG * RA/FD/FE * Milwaukee, WI (1:154/750)SEEN-BY: 396/1 632/0 371 633/260 267 270 371 634/397 635/506 728 639/252 SEEN-BY: 670/218 @PATH: 154/750 222 396/1 633/260 635/506 728 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™.