trim function dumping core

R

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
 
P

pete

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. 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 */
 
R

Richard Heathfield

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

Chris Dollin

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

rkk

Chris said:
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. 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
 
S

santosh

rkk said:
Chris said:
rkk wrote:
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.
 
C

Chris Dollin

rkk wrote:
Chris Dollin wrote:
(fx:snip)

(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 ...
 
F

Fred Kleinschmidt

Chris Dollin said:
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. 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.

What happens here?

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

CBFalconer

Chris said:
(fx:snip more)


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

Chris Dollin

Fred Kleinschmidt wrote:
(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`.)
 
T

trm

Fred said:
Chris Dollin said:
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;
}
 
R

Richard Tobin

Richard Heathfield said:
And what happens if someone asks a rhetorical question in comp.lang.c?

I don't think it's ever happened, has it?

-- Richard
 
C

christian.bau

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

Joe Wright

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. 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 */
 
R

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

rkk said:
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 */
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

trim whitespace v3 170
Trim string 42
trim whitespace 194
Comment on trim string function please 102
Fibonacci 0
trim whitespace, bullet proof version 63
validcstring function 12
Command Line Arguments 0

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top