473,787 Members | 2,798 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

trim function dumping core

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(*beg in)) {
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
31 2920
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(*beg in)) {
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\fcharacte rs\v.";

puts(trim(strin g));
return 0;
}

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

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

/* END new.c */
--
pete
Dec 6 '06 #2
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(*beg in)) {
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
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(*beg in)) {
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[] = " theLongAndWindi ngRoad";
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
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(*beg in)) {
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[] = " theLongAndWindi ngRoad";
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
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[] = " theLongAndWindi ngRoad";
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
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
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

"Chris Dollin" <ch**********@h p.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(*beg in)) {
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[] = " theLongAndWindi ngRoad";
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
Fred Kleinschmidt wrote:
"Chris Dollin" <ch**********@h p.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4958
by: Ganesh Gella | last post by:
Hi All, I am using g++ on Linux, and my code has lot of vectors each stores a particualr type of structure. (Structure internally had some vectors). When I am trying to push_back an element to a one of the vectors in the parent strutcure, it always core dumps on Linux and HP. On Solaris the same code is working fine without any problem. My code is actually an API, and this problem is seen only by few
11
4740
by: Reply Via Newsgroup | last post by:
Folks, In PHP and some other scripting languages, one has trim() - It removes newline, tabs and blank spaces that might prefix, or suffix a string. Can someone tell me how I can do this in javascript? Much appreciated, randell d.
10
4024
by: ken | last post by:
hello, i'm writing a c program on a linux system. i'm debugging a segmentation fault but i don't want it to dump a core file because the memory footprint of the program is over 300Mb and i don't need it to generate a 300Mb file every time I add a new printf statement to debug the code. can i do something to prevent it from dumping the core file even when it seg faults? (is this a unix/linux thing, or a c thing?) thanks!
17
1442
by: Michael | last post by:
Hi, Could you pleaes let me know when I need to use virtual destctor function in the base class? Thanks in advance, Michael
0
9655
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.