Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

remove leading zeros

Status
Not open for further replies.

huskers

Programmer
Jan 29, 2002
75
0
0
US
I have a short value and after performing some bit operations i use sprintf to print it to a character array. Is there any easy way to remove leading zeros.
 
sprintf(out,"%d",value);

%6d will puts leading zeroes out.
 
thanks for the reply. I allocate a string dynamically:

str=malloc(20);
str=""
for (....loop...)
{
/** here i do some calculations **/
sprintf(str,"%s%d",str,value);
}

the str1 contains 0000418.23. Is there an efficient way to remove leading zeros in str1 other than using %6d.
 
What is the value of the variable value.
and the string str?

The string shows a real number, and not an integer.
 
There are several ways to do this, here's one using a char pointer:

Code:
char *s;

s = str;
while (*s && *s == '0') s++; /* find the first non '0' element */
printf("%s\n",s); /* now use s instead of str */

Good luck.
 
To huskers:
It seems the easiest way is: where is the source of zeroes? May be you can suppress its generation from the beginning?
If not, write a function to eat leading zeroes in ASCIIZ strings (the standard way in the spirit of good old C;)...
 
1.

> str=malloc(20);
> str="";

Second assignment forgets any memory allocations, after that you can use only one first byte of literal string "". sprint()'ing to such pointer will destroy any data. Should be

str[0]=0;

2.

> sprintf(str,"%s%d",str,value);

Using of same string as source and destination is dangerous, it would produce infinite loop if the format string would be for instance " %s%%d".

3.

> the str1 contains 0000418.23

such a result could be received only in case, when on entering to sprintf() str would contain "0000418." - source of zeroes is somewhere earlier.

4. Format specifier %6d has nothing to do with leading zeroes - its purpose is to add spaces in front of the number.
 
I suspect that there is %07d specification or what else in sprintf format...
 
sprintf should not put leading zeros in the string.

itoa() is another possibilty.
 
sprintf(str,"%07d",3); results in "0000003"
sprintf(str,"%7d",3); results in " 3"
sprintf(str,"%d",3); results in "3"

But Huskers isn't using a width format specifier in his sprintf statement. It looks like He's starting with an empty string:
""
then basically concatenating the next value using sprintf:

1st loop: "0"
2nd loop: "00"
3rd loop: "000"
4th loop: "0000"
5th loop: "00004"
.
.
.
finally: "0000418.23"

(I'm still not sure where the decimal is coming from, but that's irrelevant)

The result is a numeric string with leading 0's that need to be stripped off.

Consider Eliminating the source of the leading 0's as ArkM suggested or something like I suggested earlier. There are, of course other ways to do the same thing.

Also, mingis pointed out some problems with the code.

Good luck.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top