473,799 Members | 3,390 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 2923
trm
Fred Kleinschmidt schrieb:
"Chris Dollin" <ch**********@h p.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
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
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
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
In article <8_************ ********@bt.com >,
Richard Heathfield <rj*@see.sig.in validwrote:
>And what happens if someone asks a rhetorical question in comp.lang.c?
I don't think it's ever happened, has it?

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Dec 6 '06 #15
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
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.

--
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 6 '06 #17
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;
}
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
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 #19
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(*beg in)) 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(*beg in)) 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...@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 #20

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
4025
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
9543
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,...
1
10237
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
10029
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...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7567
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
6808
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
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.