Re: trim function dumping core
- From: "santosh" <santosh.k83@xxxxxxxxx>
- Date: 6 Dec 2006 05:07:22 -0800
rkk wrote:
Chris Dollin wrote:<snip>
rkk wrote:
<snip>I've written a small trim function to trim away the whitespaces in a
given string.
<snip>Um. Er. "Small"? Don't you think that this is just a bit big
for left-and-right trimming?
Here's my effort, which I offer as a target ...
#include <stdio.h>
#include <ctype.h>
char *trim( char *s )
{
char *lastNonSpace = 0;
char *current;
while (isspace( *s )) s += 1;
current = s;
while (*current)
{
if (!isspace( *current )) lastNonSpace = current;
current += 1;
}
if (lastNonSpace) lastNonSpace[1] = 0;
return s;
}
int main( int argc, char **argv )
{
char e1[] = "";
char e2[] = "1";
char e3[] = "spoo";
char e4[] = " spoo ";
char e5[] = " spoo for tea! ";
char e6[] = " theLongAndWindingRoad";
fprintf( stderr, "'%s'\n", trim( e1 ) );
fprintf( stderr, "'%s'\n", trim( e2 ) );
fprintf( stderr, "'%s'\n", trim( e3 ) );
fprintf( stderr, "'%s'\n", trim( e4 ) );
fprintf( stderr, "'%s'\n", trim( e5 ) );
fprintf( stderr, "'%s'\n", trim( e6 ) );
return 0;
}
This code doesn't work on my machine. Seems to be broken somewhere.
It compiles fine and appears to work well enough here. A couple of
warnings about unused argc and argv are produced.
.
- References:
- trim function dumping core
- From: rkk
- Re: trim function dumping core
- From: Chris Dollin
- Re: trim function dumping core
- From: rkk
- trim function dumping core
- Prev by Date: Re: malloc
- Next by Date: Re: malloc
- Previous by thread: Re: trim function dumping core
- Next by thread: Re: trim function dumping core
- Index(es):
Relevant Pages
|
|