Problem: to eliminate initial zeroes from numbers besides those followed by '.' or alone. For example if we input a-000123+bc+0000.0008-0000+0001.07
, we will get a-123+bc+0.0008-0+1.07)
.
Using putchar and getchar only. No strings and pointers involved. I have working code, but I think it's confusing and overloaded. Can someone suggest how it might be simplified or an easier algorithm?
char curr_char = 0, prev_char = 0,flag=0;
while ((curr_char = getchar()) != '&')
{
if (isdigit(curr_char) && !(isdigit(prev_char)))
flag = 1;
if (flag)
{
while (true)
{
if (curr_char=='0'); else break;
curr_char = getchar();
}
if (curr_char == '.')putchar('0');
while (true)
{
if (isdigit(curr_char))putchar(curr_char); else break;
curr_char = getchar();
}
if (curr_char == '.')
{
putchar('.');
while (isdigit(curr_char = getchar()))putchar(curr_char);
}
flag = 0;
}
else putchar(curr_char);
prev_char = curr_char;
}