N
Netocrat
[...](e-mail address removed) 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 == '0'; i++) if (s_ini == '\0' && i > 0) {
i--;
break;
}
s_final = &s_ini;
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 == '0'; i++) ;
if (s_ini == '\0' && i > 0)
i--;
s_final = &s_ini;
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.