trim 0s

P

pemo

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
 
S

S.Tobias

pemo said:
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") ?
 
Z

Zara

pemo said:
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++;
 
C

Christopher Benson-Manica

gladiator said:
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.
 
K

Kevin Handy

pemo said:
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))

....:==)
 
B

Ben Hetland

pemo said:
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-+-
 
J

Jack Klein

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

Ben Hetland

Jack said:
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-+-
 
W

websnarf

Zara said:
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 == '0'; i++) if (s_ini == '\0' && i > 0) {
i--;
break;
}

s_final = &s_ini;

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

Old Wolf

Zara said:
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 == '0'; i++) if (s_ini == '\0' && i > 0) {
i--;
break;
}

s_final = &s_ini;


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

websnarf

Old said:
Zara said:
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 == '0'; i++) if (s_ini == '\0' && i > 0) {
i--;
break;
}

s_final = &s_ini;


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

Old Wolf

Old said:
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 == '0'; i++) if (s_ini == '\0' && i > 0) {
i--;
break;
}

s_final = &s_ini;


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?
 

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 6
how to trim() a String only at the right side? 11
trim whitespace v3 170
Tasks 1
Why does 1 represent a negative sign bit? 0
Converting an Array to a String in JavaScript 7
Trim string 42
DecimalFormat 7

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top