473,386 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

trim 0s

Can anyone suggest a nice way to trim any leading zeros off of a string,
e.g., I'd like to take a string like 00000012 and convert it to 12.

Ta

pemo
Nov 15 '05 #1
22 2087
just use printf is ok

Nov 15 '05 #2
How so?

<us******@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
just use printf is ok

Nov 15 '05 #3
pemo <us***********@gmail.com> wrote:
Can anyone suggest a nice way to trim any leading zeros off of a string,
e.g., I'd like to take a string like 00000012 and convert it to 12.

strspn(..., "0") ?

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 15 '05 #4
pemo wrote:
Can anyone suggest a nice way to trim any leading zeros off of a string,
e.g., I'd like to take a string like 00000012 and convert it to 12.

Ta

pemo

char *s_ini="00000012";
char *s_final=s_ini;
while (*s_final=='0') s_final++;

Nov 15 '05 #5
S.Tobias <si***@famous.bedbug.pals.invalid> wrote:
pemo <us***********@gmail.com> wrote:
Can anyone suggest a nice way to trim any leading zeros off of a string,
e.g., I'd like to take a string like 00000012 and convert it to 12.
strspn(..., "0") ?


size_t loc=strspn( mystring, "0" );
memmove( mystring, mystring+loc, strlen(mystring+loc)+1 );

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #6
if string consists 0's in between the characters,
we can't use strspn.

right

Nov 15 '05 #7
gladiator <gl********@gmail.com> wrote:
if string consists 0's in between the characters,
we can't use strspn.


Sure you can. strspn() returns the number of characters spanned at
the beginning of the string it is passed that match the charset.
Characters matching charset at any other point in the string are
completely inconsequential.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #8
Thank you for clarification

Nov 15 '05 #9
pemo wrote:
Can anyone suggest a nice way to trim any leading zeros off of a string,
e.g., I'd like to take a string like 00000012 and convert it to 12.

Ta

pemo


sprintf(mystring, '%d", strtol(mystring, 0, 10))

....:==)

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #10
pemo wrote:
Can anyone suggest a nice way to trim any leading zeros off of a string,
e.g., I'd like to take a string like 00000012 and convert it to 12.


sprintf(s, "%ld", atol(s));

That assumes there are nothing more than the number itself in the
string, otherwise perhaps a loop with strtok to convert each "word" in
the string separately can to the trick. Simply convert every word that
starts with '0', and just strcpy the rest.
-+-Ben-+-
Nov 15 '05 #11
On Sat, 24 Sep 2005 00:02:22 +0200, Ben Hetland
<be***********@sintef.no> wrote in comp.lang.c:
pemo wrote:
Can anyone suggest a nice way to trim any leading zeros off of a string,
e.g., I'd like to take a string like 00000012 and convert it to 12.
sprintf(s, "%ld", atol(s));


....assuming you don't mind potential undefined behavior.
That assumes there are nothing more than the number itself in the
string, otherwise perhaps a loop with strtok to convert each "word" in
the string separately can to the trick. Simply convert every word that
starts with '0', and just strcpy the rest.


Never use (or recommend, especially in this group), the ato...
conversion functions. They are old dangerous hacks, and produce
undefined behavior if the result of the conversion is outside the
range of the destination type.

The original language standard introduced the strto... functions, also
prototyped in <stdlib.h>, that have fully defined behavior for any
input other than a null pointer. And they provide for error checking
as well.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #12
Jack Klein wrote:
Never use (or recommend, especially in this group), the ato...
conversion functions. They are old dangerous hacks, and produce
undefined behavior if the result of the conversion is outside the
range of the destination type.


Yes, thank you for pointing that out!
I stand corrected!

But error checking is not quite trivial with these functions either, and
I find perhaps something like the following function a bit more
convenient in many cases (for base 10 only):

bool str_to_long_checked( const char* s, long *n )
{
long v;
char *st = 0;
errno = 0;
v = strtol(s, &st, 10);
if (st == s || errno != 0 || (st && *st=='\0'))
return false;
*n = v;
return true;
}
BTW, what's the "new" strto... equivalent of the "old" atof function then?
-+-Ben-+-
Nov 15 '05 #13

"Ben Hetland" <be***********@sintef.no> wrote in message
news:43**************@sintef.no...

BTW, what's the "new" strto... equivalent of the "old" atof function then?


strtod()

-Mike
Nov 15 '05 #14
Christopher Benson-Manica wrote:
S.Tobias <si***@famous.bedbug.pals.invalid> wrote:
pemo <us***********@gmail.com> wrote:
Can anyone suggest a nice way to trim any leading zeros off of a string,
e.g., I'd like to take a string like 00000012 and convert it to 12.

strspn(..., "0") ?


size_t loc=strspn( mystring, "0" );
memmove( mystring, mystring+loc, strlen(mystring+loc)+1 );


And what if the number is 0?

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Nov 15 '05 #15
Zara wrote:
pemo wrote:
Can anyone suggest a nice way to trim any leading zeros off of a string,
e.g., I'd like to take a string like 00000012 and convert it to 12.

Ta

pemo

char *s_ini="00000012";
char *s_final=s_ini;
while (*s_final=='0') s_final++;


Almost ... oh what the hell, might as well do it right:

for (i = 0; s_ini[i] == '0'; i++) if (s_ini[i] == '\0' && i > 0) {
i--;
break;
}

s_final = &s_ini[i];

Of course I am waiting for someone (you know who you are) to tell me
that "0" is an illegal string ...

--
Paul Hsieh
http://www.pobox.com/qed/
http://bstring.sf.net/

Nov 15 '05 #16
we******@gmail.com wrote:
Christopher Benson-Manica wrote:
S.Tobias <si***@famous.bedbug.pals.invalid> wrote:
> pemo <us***********@gmail.com> wrote:
> > Can anyone suggest a nice way to trim any leading zeros off of a string,
> > e.g., I'd like to take a string like 00000012 and convert it to 12.

> strspn(..., "0") ?


size_t loc=strspn( mystring, "0" );
memmove( mystring, mystring+loc, strlen(mystring+loc)+1 );


And what if the number is 0?

Who said anything about numbers? The OP only wanted to remove
all initial zeroes from a string. :)
http://dspace.dial.pipex.com/town/gr.../bloopers.html
"This is a lovely picture ..." is very adequate here.

Seriously, I'm sure the OP has already discovered by now
what he really needed.

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 15 '05 #17
websn...@gmail.com wrote:
Zara wrote:
pemo wrote:
Can anyone suggest a nice way to trim any leading zeros off of a
string, e.g., I'd like to take a string like 00000012 and convert
it to 12.


char *s_ini="00000012";
char *s_final=s_ini;
while (*s_final=='0') s_final++;


Almost ... oh what the hell, might as well do it right:

for (i = 0; s_ini[i] == '0'; i++) if (s_ini[i] == '\0' && i > 0) {
i--;
break;
}

s_final = &s_ini[i];


Huh? Zara's version is far better than yours, by any yardstick.
You can't write stuff like this and then expect people to not
think you are a troll...

Nov 15 '05 #18
Old Wolf wrote:
websn...@gmail.com wrote:
Zara wrote:
pemo wrote:
Can anyone suggest a nice way to trim any leading zeros off of a
string, e.g., I'd like to take a string like 00000012 and convert
it to 12.

char *s_ini="00000012";
char *s_final=s_ini;
while (*s_final=='0') s_final++;
Almost ... oh what the hell, might as well do it right:

for (i = 0; s_ini[i] == '0'; i++) if (s_ini[i] == '\0' && i > 0) {
i--;
break;
}

s_final = &s_ini[i];


Huh? Zara's version is far better than yours, by any yardstick.


Really? I pick correctness as my yardstick. Try setting s_ini =
"0000"; and compare the two solutions.
You can't write stuff like this and then expect people to not
think you are a troll...


Oh yeah, I forgot "correctness" is off topic here in CLC. If the
solution doesn't obsess over some standards minutia it must be a troll.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Nov 15 '05 #19
we******@gmail.com wrote:
Old Wolf wrote:
websn...@gmail.com wrote:
Zara wrote:
pemo wrote:
> Can anyone suggest a nice way to trim any leading zeros off of a
> string, e.g., I'd like to take a string like 00000012 and convert
> it to 12.

char *s_ini="00000012";
char *s_final=s_ini;
while (*s_final=='0') s_final++;

Almost ... oh what the hell, might as well do it right:

for (i = 0; s_ini[i] == '0'; i++) if (s_ini[i] == '\0' && i > 0) {
i--;
break;
}

s_final = &s_ini[i];
Huh? Zara's version is far better than yours, by any yardstick.


Really? I pick correctness as my yardstick. Try setting s_ini =
"0000"; and compare the two solutions.


Both solutions give "" in that case. Zara's is easier to read by far.
Oh yeah, I forgot "correctness" is off topic here in CLC. If
the solution doesn't obsess over some standards minutia it
must be a troll.


Replacing a short one-liner with a 4 lines that do exactly the
same thing in a more inefficient and obfuscated way... troll.

I suspect you are going to say that you think "0000" with leading
zeroes removed is "0", and you also planned to write a program
that gives "0", but you accidentally wrote a program that gives "".

I think this just proves the point that the shorter, clearer
solution is less prone to errors (and that you should indent
your code properly).

But even then, you would have to say that "2222" with leading
2s removed is "2", and " \t" with leading whitespace removed
is "\t", which is something that I think most people would
disagree with. Unless you think '0' is some sort of special
character?

Nov 15 '05 #20
On Tue, 27 Sep 2005 14:36:29 -0700, Old Wolf wrote:
websn...@gmail.com wrote:
Zara wrote:
> pemo wrote:
>> Can anyone suggest a nice way to trim any leading zeros off of a
>> string, e.g., I'd like to take a string like 00000012 and convert
>> it to 12.
>
> char *s_ini="00000012";
> char *s_final=s_ini;
> while (*s_final=='0') s_final++;

Almost ... oh what the hell, might as well do it right:

for (i = 0; s_ini[i] == '0'; i++) if (s_ini[i] == '\0' && i > 0) {
i--;
break;
}

s_final = &s_ini[i];
[...] I suspect you are going to say that you think "0000" with leading zeroes
removed is "0", and you also planned to write a program that gives "0",
but you accidentally wrote a program that gives "".


If so it's easily fixed and the result isn't obscure:

for (i = 0; s_ini[i] == '0'; i++) ;
if (s_ini[i] == '\0' && i > 0)
i--;
s_final = &s_ini[i];

The specification isn't clear enough to decide on correctness, but this
seems like more useful semantics when dealing solely with numeric strings
that must later be displayed as integers.

--
http://members.dodo.com.au/~netocrat
Nov 15 '05 #21
S.Tobias wrote:
we******@gmail.com wrote:
Christopher Benson-Manica wrote:
S.Tobias <si***@famous.bedbug.pals.invalid> wrote:
> pemo <us***********@gmail.com> wrote:
> > Can anyone suggest a nice way to trim any leading zeros off of a
> > string, e.g., I'd like to take a string like 00000012 and convert it
> > to 12.

> strspn(..., "0") ?

size_t loc=strspn( mystring, "0" );
memmove( mystring, mystring+loc, strlen(mystring+loc)+1 );


And what if the number is 0?

Who said anything about numbers? The OP only wanted to remove
all initial zeroes from a string. :)
http://dspace.dial.pipex.com/town/gr.../bloopers.html
"This is a lovely picture ..." is very adequate here.

Seriously, I'm sure the OP has already discovered by now
what he really needed.


If you think that sort of bug tends to eventually get sorted out in
serious applications try the following:

printf ("%f\n", 0);

.... in Microsoft Visual Studio 2003. This is the result I got:

86191978822782173000000000000000000000000000000000 000000000000000000000000000000000000000000000000.0 00000

I wish I were kidding (it prints out other numbers just fine).

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Nov 15 '05 #22
we******@gmail.com wrote:
S.Tobias wrote:
we******@gmail.com wrote:
Christopher Benson-Manica wrote:
> S.Tobias <si***@famous.bedbug.pals.invalid> wrote:
> > pemo <us***********@gmail.com> wrote:
> > > Can anyone suggest a nice way to trim any leading zeros off of a
> > > string, e.g., I'd like to take a string like 00000012 and convert it
> > > to 12.
>
> > strspn(..., "0") ?
>
> size_t loc=strspn( mystring, "0" );
> memmove( mystring, mystring+loc, strlen(mystring+loc)+1 );

And what if the number is 0?

Who said anything about numbers? The OP only wanted to remove
all initial zeroes from a string. :)
http://dspace.dial.pipex.com/town/gr.../bloopers.html
"This is a lovely picture ..." is very adequate here.

Seriously, I'm sure the OP has already discovered by now
what he really needed.


If you think that sort of bug tends to eventually get sorted out in
serious applications try the following:

printf ("%f\n", 0);

... in Microsoft Visual Studio 2003. This is the result I got:

86191978822782173000000000000000000000000000000000 000000000000000000000000000000000000000000000000.0 00000

I wish I were kidding (it prints out other numbers just fine).


Nevermind ... I just caught what I did wrong.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Nov 15 '05 #23

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

Similar topics

22
by: Simon | last post by:
Hi, I have written a function to trim char *, but I have been told that my way could be dangerous and that I should use memmove(...) instead. but I am not sure why my code could be 'dangerous'...
5
by: Alex Vassiliev | last post by:
Hi all. Just wanted to share two handy RegEx expressions to strips leading and trailing white-space from a string, and to replace all repeated spaces, newlines and tabs with a single space. *...
12
by: Robert Mark Bram | last post by:
Hi All, I am using the following trim function: function trim (str) { return str.replace(/^\s*/g, '').replace(/\s*$/g, ''); } The problem is that this doesn't trim instances of the...
3
by: FlyerN87 | last post by:
For some reason this command will not trim the names shown in the fields listed. I'm trying to do this in a text box on a report. I've done this fairly succesfully in other reports but I must...
11
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not...
7
by: Sascha Herpers | last post by:
Hi, what is the difference between the trim function and the trim String-member? As far as I see it, both return the trimmed string and leave the original string unaltered. Is any of the two...
22
by: Terry Olsen | last post by:
I have an app that makes decisions based on string content. I need to make sure that a string does not contain only spaces or newlines. I am using the syntax 'Trim(String)" and it works fine. I...
1
by: arsaral | last post by:
Hi, Here is an extension of MSDN's trim right example to trim left-right... First subroutines then the calling structure is given below... Cheers. Ali Riza SARAL ...
31
by: rkk | last post by:
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...
8
by: Keith Thompson | last post by:
Kevin Smith <no@spam.comwrites: You posted this to microsoft.public.dotnet.languages.csharp, where I presume it's topical. Why on Earth did you redirect followups to comp.lang.c? Anyone...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.