Remove white space at the end of the output in C

后端 未结 6 1307
离开以前
离开以前 2021-01-27 06:59

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 checkin

6条回答
  •  太阳男子
    2021-01-27 07:39

    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.
    

提交回复
热议问题