Home > Back-end >  How to stop printing comma and space after last digit in c
How to stop printing comma and space after last digit in c

Time:11-22

Here is the program I wrote:

int main(void)
{
    int d1, d2;

    d1 = 48;
    while (d1 < 58)
    {
        d2 = d1   1;
        while (d2 < 58)
        {
            putchar(d1);
            putchar(d2);
            putchar(',');
            putchar(' ');
            d2  ;
        }
        d1  ;
    }

    putchar(10);
    return (0);
}

Output of the program is as follows:

01, 02, 03, 04, 05, 06, 07, 08, 09, 12, 13, 14, 15, 16, 17, 18, 19, 23, 24, 25, 26, 27, 28, 29, 34, 35, 36, 37, 38, 39, 45, 46, 47, 48, 49, 56, 57, 58, 59, 67, 68, 69, 78, 79, 89, $

I want the output like this:

01, 02, 03, 04, 05, 06, 07, 08, 09, 12, 13, 14, 15, 16, 17, 18, 19, 23, 24, 25, 26, 27, 28, 29, 34, 35, 36, 37, 38, 39, 45, 46, 47, 48, 49, 56, 57, 58, 59, 67, 68, 69, 78, 79, 89$

How can I get the comma to stop before last digit?

CodePudding user response:

There are many ways to solve your problem. You can print a separator before each of the outputs except the first one.

Note also that it would be much more readable to use '0' and '9' instead of hard coding ASCII values such as 48...

Here is a modified version:

#include <stdio.h>

int main(void) {
    int d1, d2, n;

    for (n = 0, d1 = '0'; d1 <= '9'; d1  ) {
        for (d2 = d1   1; d2 <= '9'; d2  , n  ) {
            if (n > 0) {
                putchar(',');
                putchar(' ');
            }
            putchar(d1);
            putchar(d2);
        }
    }
    putchar('\n');
    return 0;
}

CodePudding user response:

A different way of doing what @chqrlie does i.e. to print separator before each character.
Before the first character the separators are null characters and for subsequent characters the separators are ',' and ' '.

Implementation:

int main(void)
{
    int d1, d2;
    int sep1 = 0;
    int sep2 = 0;

    d1 = 48;
    while (d1 < 58)
    {
        d2 = d1   1;
        while (d2 < 58)
        {
            putchar(sep1);
            putchar(sep2);
            putchar(d1);
            putchar(d2);
            d2  ;
            sep1 = ',';
            sep2 = ' ';
        }
        d1  ;
    }

    putchar(10);
    return (0);
}

CodePudding user response:

One easy way to accomplish that is to avoid printing the comma/space at the last iteration:

#include <stdio.h>

int main(void)
{
    int d1, d2;

    d1 = 48;
    while (d1 < 58)
    {
        d2 = d1   1;
        while (d2 < 58)
        {
            putchar(d1);
            putchar(d2);

            if (d1 < (58 - 2))
            {
                putchar(',');
                putchar(' ');
            }
            d2  ;
        }
        d1  ;
    }

    putchar(10);
    return (0);
}

Output:

$ gcc main.c && ./a.out
01, 02, 03, 04, 05, 06, 07, 08, 09, 12, 13, 14, 15, 16, 17, 18, 19, 23, 24, 25, 26, 27, 28, 29, 34, 35, 36, 37, 38, 39, 45, 46, 47, 48, 49, 56, 57, 58, 59, 67, 68, 69, 78, 79, 89
  • Related