By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,874 Members | 1,027 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,874 IT Pros & Developers. It's quick & easy.

trim function dumping core

P: n/a
rkk
Hi,

I've written a small trim function to trim away the whitespaces in a
given string. It works well with solaris forte cc compiler, but on
mingw/cygwin gcc it isn't. Here is the code:

char *trim(char *s)
{
char *begin,*end;
begin = s;
/* ltrim start */
while(*begin != '\0') {
if(isspace(*begin)) {
begin++;
} else {
s = begin;
break;
}
}
/* ltrim end */

/* rtrim start */
end = s + strlen(s) -1;
while( end !=s && isspace(*end) ) {
end--;
}
printf("%c\n",*end);
*(end+1) = '\0';
while(*s && isspace(*s)) s++;
/* rtrim end */
return s;
}

It is failing in the line having the code *(end+1) = '\0';. I think it
may be due to portablility issues & I need this code to work on all
well known platforms & compilers. Any sugesstions/help please?

Thank you.

Regards
Kalyan

Dec 6 '06 #1
Share this Question
Share on Google+
31 Replies


P: n/a
rkk wrote:
>
Hi,

I've written a small trim function to trim away the whitespaces in a
given string. It works well with solaris forte cc compiler, but on
mingw/cygwin gcc it isn't. Here is the code:

char *trim(char *s)
{
char *begin,*end;
begin = s;
/* ltrim start */
while(*begin != '\0') {
if(isspace(*begin)) {
begin++;
} else {
s = begin;
break;
}
}
/* ltrim end */

/* rtrim start */
end = s + strlen(s) -1;
while( end !=s && isspace(*end) ) {
end--;
}
printf("%c\n",*end);
*(end+1) = '\0';
while(*s && isspace(*s)) s++;
/* rtrim end */
return s;
}

It is failing in the line having the code *(end+1) = '\0';. I think it
may be due to portablility issues & I need this code to work on all
well known platforms & compilers. Any sugesstions/help please?
/* BEGIN new.c */

#include <stdio.h>
#include <ctype.h>

char *trim(char *s1);

int main(void)
{
char string[] = "This is a string with tabs \t\t and spaces"
" and\rother space\fcharacters\v.";

puts(trim(string));
return 0;
}

char *trim(char *s1)
{
char *const p1 = s1;
char *p2;

for (p2 = s1; *s1 != '\0'; ++s1) {
if (!isspace((unsigned char)*s1)) {
*p2++ = *s1;
}
}
*p2 = '\0';
return p1;
}

/* END new.c */
--
pete
Dec 6 '06 #2

P: n/a
rkk said:
Hi,

I've written a small trim function to trim away the whitespaces in a
given string. It works well with solaris forte cc compiler, but on
mingw/cygwin gcc it isn't.
Could you please provide some test data that is known to fail? In the
meantime, let's try it with the empty string - always a good test.
Here is the code:
....and your tabbed indenting was stripped out somewhere along the line -
probably by my client. I've mended it kinda...
>
char *trim(char *s)
{
char *begin,*end;
begin = s;
/* ltrim start */
while(*begin != '\0') {
if(isspace(*begin)) {
begin++;
} else {
s = begin;
break;
}
}
/* ltrim end */
Okay, the empty string will skip right around that first loop, and go on to
here...
/* rtrim start */
end = s + strlen(s) -1;
Whoops, there's a bug already. s + 0 - 1 is s - 1, which is an invalid
pointer. Fix that, and let us know if you still have problems. If so, give
us the latest code and some test data that is known to fail.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 6 '06 #3

P: n/a
rkk wrote:
Hi,

I've written a small trim function to trim away the whitespaces in a
given string. It works well with solaris forte cc compiler, but on
mingw/cygwin gcc it isn't. Here is the code:

char *trim(char *s)
{
char *begin,*end;
begin = s;
/* ltrim start */
while(*begin != '\0') {
if(isspace(*begin)) {
begin++;
} else {
s = begin;
break;
}
}
/* ltrim end */

/* rtrim start */
end = s + strlen(s) -1;
while( end !=s && isspace(*end) ) {
end--;
}
printf("%c\n",*end);
*(end+1) = '\0';
while(*s && isspace(*s)) s++;
/* rtrim end */
return s;
}
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;
}

Notes:

`0` aka `'\0'` is not a space character. Hence the first loop
will terminate safely, at either the end of the string or
on its first non-space character.

If `lastNonSpace` is not null, it is always legal to
assign to `lastNonSpace[1]`. Either the last non-space
was the last character of the string, in which case
`lastNonSpace+1` points to the terminating null, or
it isn't, in which case `lastNonSpace+1` points to
a space.

This `trim` updates the passed string. Hence it must
not be used on string literals. Hence the examples
in `main` are string /array/ objects /initialised
from/ string literals.

--
Chris "Perikles triumphant" Dollin
Meaning precedes definition.

Dec 6 '06 #4

P: n/a
rkk

Chris Dollin wrote:
rkk wrote:
Hi,

I've written a small trim function to trim away the whitespaces in a
given string. It works well with solaris forte cc compiler, but on
mingw/cygwin gcc it isn't. Here is the code:

char *trim(char *s)
{
char *begin,*end;
begin = s;
/* ltrim start */
while(*begin != '\0') {
if(isspace(*begin)) {
begin++;
} else {
s = begin;
break;
}
}
/* ltrim end */

/* rtrim start */
end = s + strlen(s) -1;
while( end !=s && isspace(*end) ) {
end--;
}
printf("%c\n",*end);
*(end+1) = '\0';
while(*s && isspace(*s)) s++;
/* rtrim end */
return s;
}

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;
}

Notes:

`0` aka `'\0'` is not a space character. Hence the first loop
will terminate safely, at either the end of the string or
on its first non-space character.

If `lastNonSpace` is not null, it is always legal to
assign to `lastNonSpace[1]`. Either the last non-space
was the last character of the string, in which case
`lastNonSpace+1` points to the terminating null, or
it isn't, in which case `lastNonSpace+1` points to
a space.

This `trim` updates the passed string. Hence it must
not be used on string literals. Hence the examples
in `main` are string /array/ objects /initialised
from/ string literals.

Chris "Perikles triumphant" Dollin
Meaning precedes definition.
This code doesn't work on my machine. Seems to be broken somewhere.

Thanks for your effort.

Cheers
Kalyan

Dec 6 '06 #5

P: n/a
rkk wrote:
Chris Dollin wrote:
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;
}
<snip>
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.

Dec 6 '06 #6

P: n/a
rkk said:

<snip>
>
This code doesn't work on my machine.
Symptoms?
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 6 '06 #7

P: n/a
rkk wrote:
Chris Dollin wrote:
(fx:snip)
>#include <stdio.h>
#include <ctype.h>

char *trim( char *s )
(fx:snip more)
This code doesn't work on my machine. Seems to be broken somewhere.
Hell's teeth, man, can't you be more explicit? What were the
symptoms? What implementation are you using? What, if any,
were the compiler messages? What was the output? Did you compile
the code exactly as I posted it?

If there's a mistake in it, I'd like to know where, but since
my crystal ball is back being repaired right now, I don't
know where to start ...

--
Chris "Perikles triumphant" Dollin
"We did not have time to find out everything we wanted to know."
- James Blish, /A Clash of Cymbals/

Dec 6 '06 #8

P: n/a

"Chris Dollin" <ch**********@hp.comwrote in message
news:el**********@murdoch.hpl.hp.com...
rkk wrote:
>Hi,

I've written a small trim function to trim away the whitespaces in a
given string. It works well with solaris forte cc compiler, but on
mingw/cygwin gcc it isn't. Here is the code:

char *trim(char *s)
{
char *begin,*end;
begin = s;
/* ltrim start */
while(*begin != '\0') {
if(isspace(*begin)) {
begin++;
} else {
s = begin;
break;
}
}
/* ltrim end */

/* rtrim start */
end = s + strlen(s) -1;
while( end !=s && isspace(*end) ) {
end--;
}
printf("%c\n",*end);
*(end+1) = '\0';
while(*s && isspace(*s)) s++;
/* rtrim end */
return s;
}

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;
}

Notes:

`0` aka `'\0'` is not a space character. Hence the first loop
will terminate safely, at either the end of the string or
on its first non-space character.

If `lastNonSpace` is not null, it is always legal to
assign to `lastNonSpace[1]`. Either the last non-space
was the last character of the string, in which case
`lastNonSpace+1` points to the terminating null, or
it isn't, in which case `lastNonSpace+1` points to
a space.

This `trim` updates the passed string. Hence it must
not be used on string literals. Hence the examples
in `main` are string /array/ objects /initialised
from/ string literals.

--
Chris "Perikles triumphant" Dollin
Meaning precedes definition.
What happens here?

int main( int argc, char **argv ) {
fprintf( stderr, "'%s'\n", trim(NULL) );
return 0;
}

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project


Dec 6 '06 #9

P: n/a
Fred Kleinschmidt wrote:
"Chris Dollin" <ch**********@hp.comwrote in message
(fx:snip)
>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;
}
(fx:snip more)
What happens here?

int main( int argc, char **argv ) {
fprintf( stderr, "'%s'\n", trim(NULL) );
return 0;
}
Undefined behaviour. I should have documented that `trim` expects
a string. (So does the OPs `trim`.)

--
Chris "Perikles triumphant" Dollin
"It took a very long time, much longer than the most generous estimates."
- James White, /Sector General/

Dec 6 '06 #10

P: n/a
trm
Fred Kleinschmidt schrieb:
"Chris Dollin" <ch**********@hp.comwrote in message
news:el**********@murdoch.hpl.hp.com...
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;
}
What happens here?

int main( int argc, char **argv ) {
fprintf( stderr, "'%s'\n", trim(NULL) );
return 0;
}
What happens here?

#include <string.h>
int main ( void )
{
strlen(NULL);
return 0;
}

Dec 6 '06 #11

P: n/a
trm wrote:
What happens here?

#include <string.h>
int main ( void )
{
strlen(NULL);
return 0;
}
Possibly anything. It invokes undefined behaviour.

Dec 6 '06 #12

P: n/a
Chris Dollin wrote:
rkk wrote:
>Chris Dollin wrote:

(fx:snip)
>>#include <stdio.h>
#include <ctype.h>

char *trim( char *s )

(fx:snip more)
>This code doesn't work on my machine. Seems to be broken somewhere.

Hell's teeth, man, can't you be more explicit? What were the
symptoms? What implementation are you using? What, if any,
were the compiler messages? What was the output? Did you compile
the code exactly as I posted it?

If there's a mistake in it, I'd like to know where, but since
my crystal ball is back being repaired right now, I don't
know where to start ...
Seems to work fine here.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Dec 6 '06 #13

P: n/a
santosh said:
trm wrote:
>What happens here?

#include <string.h>
int main ( void )
{
strlen(NULL);
return 0;
}

Possibly anything. It invokes undefined behaviour.
And what happens if someone asks a rhetorical question in comp.lang.c?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 6 '06 #14

P: n/a
In article <8_********************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>And what happens if someone asks a rhetorical question in comp.lang.c?
I don't think it's ever happened, has it?

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Dec 6 '06 #15

P: n/a
Richard Heathfield wrote:
santosh said:
trm wrote:
What happens here?

#include <string.h>
int main ( void )
{
strlen(NULL);
return 0;
}
Possibly anything. It invokes undefined behaviour.

And what happens if someone asks a rhetorical question in comp.lang.c?
Damn! I should stop taking myself so seriously.

Dec 6 '06 #16

P: n/a
Richard Heathfield <rj*@see.sig.invalidwrites:
[...]
And what happens if someone asks a rhetorical question in comp.lang.c?
Some idiot invariably answers it.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 6 '06 #17

P: n/a
rkk wrote:
Hi,

I've written a small trim function to trim away the whitespaces in a
given string. It works well with solaris forte cc compiler, but on
mingw/cygwin gcc it isn't. Here is the code:

char *trim(char *s)
{
char *begin,*end;
begin = s;
/* ltrim start */
while(*begin != '\0') {
if(isspace(*begin)) {
begin++;
} else {
s = begin;
break;
}
}
/* ltrim end */

/* rtrim start */
end = s + strlen(s) -1;
while( end !=s && isspace(*end) ) {
end--;
}
printf("%c\n",*end);
*(end+1) = '\0';
while(*s && isspace(*s)) s++;
/* rtrim end */
return s;
}
There are no compatibility issues, the code is just plainly broken.
Have a careful look what happens when you pass in an empty string.

Dec 6 '06 #18

P: n/a
rkk wrote:
Hi,

I've written a small trim function to trim away the whitespaces in a
given string. It works well with solaris forte cc compiler, but on
mingw/cygwin gcc it isn't. Here is the code:

char *trim(char *s)
{
char *begin,*end;
begin = s;
/* ltrim start */
while(*begin != '\0') {
if(isspace(*begin)) {
begin++;
} else {
s = begin;
break;
}
}
/* ltrim end */

/* rtrim start */
end = s + strlen(s) -1;
while( end !=s && isspace(*end) ) {
end--;
}
printf("%c\n",*end);
*(end+1) = '\0';
while(*s && isspace(*s)) s++;
/* rtrim end */
return s;
}

It is failing in the line having the code *(end+1) = '\0';. I think it
may be due to portablility issues & I need this code to work on all
well known platforms & compilers. Any sugesstions/help please?
Here's mine..

/************************************************** ******************
Program: trim.c
Author: Joe Wright
Date: 12/06/2006

rtrim() and ltrim() functions a la dBASE modify string in place.

************************************************** *******************/

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define length 256 /* Limit lines to 255 characters */
#define trim rtrim /* trim() is synonomous with rtrim() */

/* Remove trailing whitespace */

char * rtrim(char *str) {
char *s, *p; int c;
s = p = str;
while ((c = *s++)) if (!isspace(c)) p = s;
*p = '\0';
return str;
}

/* Remove leading whitespace */

char * ltrim(char *str) {
char *s, *p;
for (s = p = str; *s && isspace(*s); s++) ;
while ((*p++ = *s++)) ;
return str;
}

/* Combination of the two */

char * alltrim(char *str) {
return rtrim(ltrim(str));
}

/* A modest test */

int main(int argc, char **argv) {
char str[length];

if (argc != 2) {
printf("Usage: trim ' Try something like this. '\n");
return 0;
}

if (strlen(argv[1]) length-1) {
printf("Input String Too Long!\n");
return 0;
}
strcpy(str, argv[1]);
printf(" Input: Length %2d '%s'\n", (int)strlen(str), str);

ltrim(str);
printf(" ltrim: Length %2d '%s'\n", (int)strlen(str), str);

strcpy(str, argv[1]);
rtrim(str);
printf(" rtrim: Length %2d '%s'\n", (int)strlen(str), str);

strcpy(str, argv[1]);
alltrim(str);
printf("alltrim: Length %2d '%s'\n", (int)strlen(str), str);

return 0;
}
/* End of trim.c */
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Dec 7 '06 #19

P: n/a
rkk
All,

Thank you for your code snippets & help provided. Here is my code which
works on Linux, Solaris & windows:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_STR_LEN 4000

char *trim(char *s);
char *rtrim(char *s);
char *ltrim(char *s);

char *trim(char *s)
{
char t[MAX_STR_LEN];
char *begin,*end;
strcpy(t,s);
begin = t;

while(*begin != '\0') {
if(isspace(*begin)) begin++;
else {
strcpy(t , begin);
break;
}
}

end = t + strlen(t) - 1;
while(end != t && isspace(*end)) end--;
*(end + 1) = '\0';
s = t;

return s;
}

char *ltrim(char *s)
{
char *begin;
begin = s;
while(*begin != '\0') {
if(isspace(*begin)) begin++;
else {
s = begin;
break;
}
}
return s;
}

char *rtrim(char *s)
{
char t[MAX_STR_LEN];
char *end;
strcpy(t,s);
end = t + strlen(t) - 1;
while(end != t && isspace(*end)) end--;
*(end + 1) = '\0';
s = t;

return s;
}

int main()
{
char *str = " sample string ";
printf("[%s]\n",str);
printf("ltrim [%s]\n",ltrim(str));
printf("rtrim [%s]\n",rtrim(str));
printf("trim [%s]\n",trim(str));

return 0;
}
Thanks & Regards
RKK

On Dec 7, 6:19 am, Joe Wright <joewwri...@comcast.netwrote:
rkk wrote:
Hi,
I've written a small trim function to trim away the whitespaces in a
given string. It works well with solaris forte cc compiler, but on
mingw/cygwin gcc it isn't. Here is the code:
char *trim(char *s)
{
char *begin,*end;
begin = s;
/* ltrim start */
while(*begin != '\0') {
if(isspace(*begin)) {
begin++;
} else {
s = begin;
break;
}
}
/* ltrim end */
/* rtrim start */
end = s + strlen(s) -1;
while( end !=s && isspace(*end) ) {
end--;
}
printf("%c\n",*end);
*(end+1) = '\0';
while(*s && isspace(*s)) s++;
/* rtrim end */
return s;
}
It is failing in the line having the code *(end+1) = '\0';. I think it
may be due to portablility issues & I need this code to work on all
well known platforms & compilers. Any sugesstions/help please?Here's mine..

/************************************************** ******************
Program: trim.c
Author: Joe Wright
Date: 12/06/2006

rtrim() and ltrim() functions a la dBASE modify string in place.

************************************************** *******************/

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define length 256 /* Limit lines to 255 characters */
#define trim rtrim /* trim() is synonomous with rtrim() */

/* Remove trailing whitespace */

char * rtrim(char *str) {
char *s, *p; int c;
s = p = str;
while ((c = *s++)) if (!isspace(c)) p = s;
*p = '\0';
return str;

}/* Remove leading whitespace */

char * ltrim(char *str) {
char *s, *p;
for (s = p = str; *s && isspace(*s); s++) ;
while ((*p++ = *s++)) ;
return str;

}/* Combination of the two */

char * alltrim(char *str) {
return rtrim(ltrim(str));

}/* A modest test */

int main(int argc, char **argv) {
char str[length];

if (argc != 2) {
printf("Usage: trim ' Try something like this. '\n");
return 0;
}

if (strlen(argv[1]) length-1) {
printf("Input String Too Long!\n");
return 0;
}
strcpy(str, argv[1]);
printf(" Input: Length %2d '%s'\n", (int)strlen(str), str);

ltrim(str);
printf(" ltrim: Length %2d '%s'\n", (int)strlen(str), str);

strcpy(str, argv[1]);
rtrim(str);
printf(" rtrim: Length %2d '%s'\n", (int)strlen(str), str);

strcpy(str, argv[1]);
alltrim(str);
printf("alltrim: Length %2d '%s'\n", (int)strlen(str), str);

return 0;}/* End of trim.c */

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Dec 7 '06 #20

P: n/a
rkk
Dear Mr.Joe,
Thanks for your code.

Regards
RKK
On Dec 7, 6:19 am, Joe Wright <joewwri...@comcast.netwrote:
rkk wrote:
Hi,
I've written a small trim function to trim away the whitespaces in a
given string. It works well with solaris forte cc compiler, but on
mingw/cygwin gcc it isn't. Here is the code:
char *trim(char *s)
{
char *begin,*end;
begin = s;
/* ltrim start */
while(*begin != '\0') {
if(isspace(*begin)) {
begin++;
} else {
s = begin;
break;
}
}
/* ltrim end */
/* rtrim start */
end = s + strlen(s) -1;
while( end !=s && isspace(*end) ) {
end--;
}
printf("%c\n",*end);
*(end+1) = '\0';
while(*s && isspace(*s)) s++;
/* rtrim end */
return s;
}
It is failing in the line having the code *(end+1) = '\0';. I think it
may be due to portablility issues & I need this code to work on all
well known platforms & compilers. Any sugesstions/help please?Here's mine..

/************************************************** ******************
Program: trim.c
Author: Joe Wright
Date: 12/06/2006

rtrim() and ltrim() functions a la dBASE modify string in place.

************************************************** *******************/

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define length 256 /* Limit lines to 255 characters */
#define trim rtrim /* trim() is synonomous with rtrim() */

/* Remove trailing whitespace */

char * rtrim(char *str) {
char *s, *p; int c;
s = p = str;
while ((c = *s++)) if (!isspace(c)) p = s;
*p = '\0';
return str;

}/* Remove leading whitespace */

char * ltrim(char *str) {
char *s, *p;
for (s = p = str; *s && isspace(*s); s++) ;
while ((*p++ = *s++)) ;
return str;

}/* Combination of the two */

char * alltrim(char *str) {
return rtrim(ltrim(str));

}/* A modest test */

int main(int argc, char **argv) {
char str[length];

if (argc != 2) {
printf("Usage: trim ' Try something like this. '\n");
return 0;
}

if (strlen(argv[1]) length-1) {
printf("Input String Too Long!\n");
return 0;
}
strcpy(str, argv[1]);
printf(" Input: Length %2d '%s'\n", (int)strlen(str), str);

ltrim(str);
printf(" ltrim: Length %2d '%s'\n", (int)strlen(str), str);

strcpy(str, argv[1]);
rtrim(str);
printf(" rtrim: Length %2d '%s'\n", (int)strlen(str), str);

strcpy(str, argv[1]);
alltrim(str);
printf("alltrim: Length %2d '%s'\n", (int)strlen(str), str);

return 0;}/* End of trim.c */

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Dec 7 '06 #21

P: n/a
Joe/rkk,

check following main with your ltrim/rtrim/trim/trim_all...

int main()
{
char *p=NULL;
ltrim(p);

rtrim(p);

return 0;
}

I am not sure what should be the behaviour of l/r/all trim against NULL
ptr, on my sys is Seg Fault.(I am knowing why)

Should we check ?

char* (l/r) trim (char *s)
{
if (s==NULL) return NULL;
your remaining code of ltrim/rtrim;
}

this will ensure that your trim function is stable against one more
type of Bad-Input, (like one more testcase)
Even it is more likely that program may crash, but not in
ltrim/rtrim/all_trim written by you.

any thought on this ?
--raxit sheth

Dec 7 '06 #22

P: n/a
Richard Heathfield wrote:
santosh said:
>trm wrote:
>>What happens here?

#include <string.h>
int main ( void )
{
strlen(NULL);
return 0;
}

Possibly anything. It invokes undefined behaviour.

And what happens if someone asks a rhetorical question in comp.lang.c?
Someone points out that rhetoric is off-topic, don't they?

--
Chris "Perikles triumphant" Dollin
"A facility for quotation covers the absence of original thought." /Gaudy Night/

Dec 7 '06 #23

P: n/a
Keith Thompson wrote:
Richard Heathfield <rj*@see.sig.invalidwrites:
[...]
And what happens if someone asks a rhetorical question in comp.lang.c?

Some idiot invariably answers it.
So you're implying obtusely that I'm an idiot?

Dec 7 '06 #24

P: n/a
"santosh" <sa*********@gmail.comwrites:
Keith Thompson wrote:
>Richard Heathfield <rj*@see.sig.invalidwrites:
[...]
And what happens if someone asks a rhetorical question in comp.lang.c?

Some idiot invariably answers it.

So you're implying obtusely that I'm an idiot?
I'm not going to answer that. 8-)}

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 7 '06 #25

P: n/a
santosh said:
Keith Thompson wrote:
>Richard Heathfield <rj*@see.sig.invalidwrites:
[...]
And what happens if someone asks a rhetorical question in comp.lang.c?

Some idiot invariably answers it.

So you're implying obtusely that I'm an idiot?
It is possible to infer that Keith is being self-deprecatingly self-mocking,
and in fact that is what I inferred when I read his reply. Don't jump at
shadows!

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 7 '06 #26

P: n/a
Richard Heathfield <rj*@see.sig.invalidwrites:
santosh said:
>Keith Thompson wrote:
>>Richard Heathfield <rj*@see.sig.invalidwrites:
[...]
And what happens if someone asks a rhetorical question in comp.lang.c?

Some idiot invariably answers it.

So you're implying obtusely that I'm an idiot?

It is possible to infer that Keith is being self-deprecatingly
self-mocking, and in fact that is what I inferred when I read his
reply.
It is, in fact, certain.
Don't jump at shadows!
I assumed that santosh was *probably* joking. I wasn't certain, which
is why I added a smiley to my reply. But to be as clear as possible,
no serious insult was intended, and any non-serious insult was
directed only at myself. (*I* was the idiot who was answering a
rhetorical question.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 7 '06 #27

P: n/a
ra************@yahoo.co.in wrote:
Joe/rkk,

check following main with your ltrim/rtrim/trim/trim_all...

int main()
{
char *p=NULL;
ltrim(p);

rtrim(p);

return 0;
}

I am not sure what should be the behaviour of l/r/all trim against NULL
ptr, on my sys is Seg Fault.(I am knowing why)

Should we check ?

char* (l/r) trim (char *s)
{
if (s==NULL) return NULL;
your remaining code of ltrim/rtrim;
}

this will ensure that your trim function is stable against one more
type of Bad-Input, (like one more testcase)
Even it is more likely that program may crash, but not in
ltrim/rtrim/all_trim written by you.

any thought on this ?
--raxit sheth
String functions should behave gracefully when given strings to deal
with. Even zero length strings. It is up to the caller to ensure against
NULL pointers passed to string functions, IMO.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Dec 7 '06 #28

P: n/a
Joe Wright wrote:
>
ra************@yahoo.co.in wrote:
Joe/rkk,

check following main with your ltrim/rtrim/trim/trim_all...

int main()
{
char *p=NULL;
String functions should behave gracefully when given strings to deal
with. Even zero length strings.
It is up to the caller to ensure against
NULL pointers passed to string functions, IMO.
That's the way it is with the string functions
in the standard library.

--
pete
Dec 8 '06 #29

P: n/a
rkk
Sheth,

Yes you are right.
The code check for NULL should be done in ltrim,trim & rtrim functions.

Regards
RKK

ra************@yahoo.co.in wrote:
Joe/rkk,

check following main with your ltrim/rtrim/trim/trim_all...

int main()
{
char *p=NULL;
ltrim(p);

rtrim(p);

return 0;
}

I am not sure what should be the behaviour of l/r/all trim against NULL
ptr, on my sys is Seg Fault.(I am knowing why)

Should we check ?

char* (l/r) trim (char *s)
{
if (s==NULL) return NULL;
your remaining code of ltrim/rtrim;
}

this will ensure that your trim function is stable against one more
type of Bad-Input, (like one more testcase)
Even it is more likely that program may crash, but not in
ltrim/rtrim/all_trim written by you.

any thought on this ?
--raxit sheth
Dec 8 '06 #30

P: n/a


Hi,
If this code works on Linux, Solaris, and Windows, than the day you wrote
that code was one of your luckiest days ;-))

On Thu, 6 Dec 2006, rkk wrote:
All,

Thank you for your code snippets & help provided. Here is my code which
works on Linux, Solaris & windows:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_STR_LEN 4000

char *trim(char *s);
char *rtrim(char *s);
char *ltrim(char *s);

char *trim(char *s)
{
char t[MAX_STR_LEN];
<SNIPPED>
s = t;

return s;
Here you are returning a pointer to a local variable (the local array t).
The memory has been released when the function returns. The fact that it
still contains your original data and is even accessible at all is a mere
coincidence.
}

Also if you have such arbitrary limitations on the lengths of strings that
you accept (less than MAX_STR_LEN), you should check that the caller does
not pass strings larger than MAX_STR_LEN, and fail if it does so.
Emil

<SNIPPED>

Dec 8 '06 #31

P: n/a
rkk
Hi Dan,
Thanks for pointing it out. Here is the corrected code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_STR_LEN 4000

char *trim(char *s);
char *rtrim(char *s);
char *ltrim(char *s);

char *trim(char *s)
{
char *begin,*end;
begin = s;

while(*begin != '\0') {
if(isspace(*begin)) begin++;
else {
s = begin;
break;
}
}

end = s + strlen(s) - 1;
while(end != s && isspace(*end)) end--;
*(end + 1) = '\0';
while (*s && isspace(*s)) s++;
return s;
}
char *ltrim(char *s)
{
char *begin;
begin = s;
while(*begin != '\0') {
if(isspace(*begin)) begin++;
else {
s = begin;
break;
}
}
return s;
}

char *rtrim(char *s)
{
char *end;
end = s + strlen(s) - 1;
while(end != s && isspace(*end)) end--;
*(end + 1) = '\0';
while (*s && isspace(*s)) s++;
return s;
}

int main()
{
char str[MAX_STR_LEN] = " sample string ";
printf("Before trim str= [%s]\n",str);
strcpy(str,trim(str));
printf("After trim str= [%s]\n",str);
return 0;
}

Regards
RKK
Kohn Emil Dan wrote:
Hi,
If this code works on Linux, Solaris, and Windows, than the day you wrote
that code was one of your luckiest days ;-))

On Thu, 6 Dec 2006, rkk wrote:
All,

Thank you for your code snippets & help provided. Here is my code which
works on Linux, Solaris & windows:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_STR_LEN 4000

char *trim(char *s);
char *rtrim(char *s);
char *ltrim(char *s);

char *trim(char *s)
{
char t[MAX_STR_LEN];

<SNIPPED>
s = t;

return s;

Here you are returning a pointer to a local variable (the local array t).
The memory has been released when the function returns. The fact that it
still contains your original data and is even accessible at all is a mere
coincidence.
}


Also if you have such arbitrary limitations on the lengths of strings that
you accept (less than MAX_STR_LEN), you should check that the caller does
not pass strings larger than MAX_STR_LEN, and fail if it does so.
Emil

<SNIPPED>
Dec 12 '06 #32

This discussion thread is closed

Replies have been disabled for this discussion.