Remove white space at the end of the output in C

2019-03-03 10:22发布

The following code is for printing the elements of a matrix in spiral order. The program works fine. The problem, however, is that the online compiler against which I'm checking the program, doesn't accept trailing white spaces at the end of the output. Could anyone give me some ideas as to how I can get around the last white space being added at the output?

For reference, my code is as follows (yes the variable names are terrible. I'm working on changing my habit of putting random variable names!!)

#include <stdio.h>
int main()
{
    int a[6][6];
    int i, k = 0, l = 0, m=3, n=3, j;
    scanf("%d %d",&m, &n);
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
            scanf("%d",&a[i][j]);
    }
    while (k < m && l < n)
    {
        for (i = l; i < n; ++i)
            printf("%d ", a[k][i]);
        k++;
        for (i = k; i < m; ++i)
            printf("%d ", a[i][n-1]);
        n--;
        if ( k < m)
        {
            for (i = n-1; i >= l; --i)
                printf("%d ", a[m-1][i]);
            m--;
        }
        if (l < n)
        {
            for (i = m-1; i >= k; --i)
                printf("%d ", a[i][l]);
            l++;
        }
    }
    return 0;
}

Input:

1 2 3

4 5 6

7 8 9

Output:

1 2 3 6 9 8 7 4 5{one extra space}

Any way to fix this problem? (Also sorry for the terrible formatting. First question on StackOverflow!)

标签: c whitespace
6条回答
Ridiculous、
2楼-- · 2019-03-03 10:45

Looking at your code, this for (the first one in the while loop):

for (i = l; i < n; ++i)
    printf("%d ", a[k][i]);

will always be executed at least ones (because l<n, coming from the while's condition).

Then you can just do the following:

  • always add the space in front of the number
  • add a single if check just for this very first for-loop (use some bool flag).

For example, something like:

bool first = true;
while (k < m && l < n)
{
    for (i = l; i < n; ++i)
    {
        if( ! first )
        {
            printf(" %d", a[k][i]);
        }
        else
        {
            printf("%d", a[k][i]);
            first = false;
        }
    }
    // ....
}

This will be rather efficient and short solution - the if is in just one loop and the flag will be true just once (will avoid cache misses).

查看更多
女痞
3楼-- · 2019-03-03 10:47

You can put an if condition in your for loops

for (i = l; i < n; ++i)
{
        printf("%d", a[k][i]);
        if(i < n-1)
            printf(" ");
}
查看更多
姐就是有狂的资本
4楼-- · 2019-03-03 10:47

You can set a boolean variable isFirst to true before your printing out any stuff, and test it before each printf statement. If isFirst, do not print a space but set isFirst to false; else print a single space. After that, continue with printing your number without a space.

Alternative: Instead of printing your results immediately, create a results array. Store your results in there, and when done, print out the results in a tight loop. You can print the first number without a leading space, then loop over the remainder and print them with a leading space.

查看更多
别忘想泡老子
5楼-- · 2019-03-03 10:47

the printf() statement,

 printf("%d ", a[k][i]);

results in extra space. use

"%d" without space or use space in the begining as,

" %d"

then at the end there wont be a extra space.
its about how you use space in your printf(). use space in a way that extra space is not present at the end as you wanted.
You can use code like this,

while (k < m && l < n)
    {
        for (i = l; i < n; ++i)
        {
            if(l==0&&i==0)
            {
              printf("%d", a[k][i]);
            }
            else
              printf(" %d", a[k][i]);
        }
        k++;
        for (i = k; i < m; ++i)
            printf(" %d", a[i][n-1]);
        n--;
        if ( k < m)
        {
            for (i = n-1; i >= l; --i)
                printf(" %d", a[m-1][i]);
            m--;
        }
        if (l < n)
        {
            for (i = m-1; i >= k; --i)
                printf(" %d", a[i][l]);
            l++;
        }
查看更多
孤傲高冷的网名
6楼-- · 2019-03-03 10:54

You need to suppress the space when you don't need one. You can do it like this:

Add these declarations:

char *format = "%d";
int first_number = 1;

Add this after the first printf:

if (first_number) {
    /* Now we want a space between numbers */
    first_number = 0;
    format = " %d";
}

Change your printf:s to use the new variable:

            printf(format, ...);
查看更多
forever°为你锁心
7楼-- · 2019-03-03 11:03

An answer after accepted answer:

Rather than:

while (k < m && l < n) {
  ...
  printf("%d ", a[k][i]);
  ...
  printf("%d ", a[i][n-1]);
  ...
  printf("%d ", a[m-1][i]);
  ...
  printf("%d ", a[i][l]);
  ...
}

Change the format.

const char *format = "%d";
while (k < m && l < n) {
  ...
  printf(format, a[k][i]);
  format = " %d";
  ...
  printf(format, a[i][n-1]);
  ...
  printf(format, a[m-1][i]);
  ...
  printf(format, a[i][l]);
  ...
}
fputc('\n', stdout); // if desired.
查看更多
登录 后发表回答