473,799 Members | 3,270 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
31 2925
rkk
Dear Mr.Joe,
Thanks for your code.

Regards
RKK
On Dec 7, 6:19 am, Joe Wright <joewwri...@com cast.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(*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?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
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
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
Keith Thompson wrote:
Richard Heathfield <rj*@see.sig.in validwrites:
[...]
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
"santosh" <sa*********@gm ail.comwrites:
Keith Thompson wrote:
>Richard Heathfield <rj*@see.sig.in validwrites:
[...]
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_Keit h) 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
santosh said:
Keith Thompson wrote:
>Richard Heathfield <rj*@see.sig.in validwrites:
[...]
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
Richard Heathfield <rj*@see.sig.in validwrites:
santosh said:
>Keith Thompson wrote:
>>Richard Heathfield <rj*@see.sig.in validwrites:
[...]
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_Keit h) 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
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
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
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

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
4742
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
4026
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
10490
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...
1
10238
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10030
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
7570
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
6809
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
5467
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...
0
5589
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4145
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2941
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.