Scanf does not close the read

Asked

Viewed 74 times

0

I am trying to make a program where it receives a char matrix and at the end prints the new matrix.

#include <stdio.h>

int main(){
    int l,c,i,j;

    scanf("%d %d",&l,&c);

    char matriz[l][c],matriz_aux[l][c];

    for(i = 0; i < l; i++){
        for(j = 0; j < c; j++){
            scanf("%s",&matriz[i][j]);
        }
    }

    for(i = 0; i < l; i++){
        for(j = 0; j < c; j++){
            if(matriz[i][j] == '.'){
                matriz_aux[i][j] = 'A';
            }else{
                matriz_aux[i][j] = matriz[i][j];
            }
        }
    }

    for(i = 0; i < l; i++){
        for(j = 0; j < c; j++){
             printf("%c", matriz_aux[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Input example:

10 3
..#
#.#
...
.#.
.##
...
...
#..
..#
#.#

Expected output for this input:

AA#
#A#
AAA
A#A
A##
AAA
AAA
#AA
AA#
#A#
  • 2

    You’re reading every cell with %s, which apparently you wish you’d used, actually, %c

  • 2

    Or else use %s to read all the line

  • Yes... With the %s the input would only close if the items were duplicated

2 answers

2

instead of %s use %c in the matrix scanf.

#include <stdio.h>

    int main(){
        int l,c,i,j;

        scanf("%d %d",&l,&c);

        char matriz[l][c],matriz_aux[l][c];

        for(i = 0; i < l; i++){
            for(j = 0; j < c; j++){
                scanf("%c",&matriz[i][j]);
            }
        }

        for(i = 0; i < l; i++){
            for(j = 0; j < c; j++){
                if(matriz[i][j] == '.'){
                    matriz_aux[i][j] = 'A';
                }else{
                    matriz_aux[i][j] = matriz[i][j];
                }
            }
        }

        for(i = 0; i < l; i++){
            for(j = 0; j < c; j++){
                 printf("%c", matriz_aux[i][j]);
            }
            printf("\n");
        }

        return 0;
    }
  • It is possible to use %s and already read the whole line in matrix[i], as I did in my reply.

2


scanf with %s will read the whole line, so it would be enough l loops. Also, unless you will need matriz_aux for something else it is superfluous and one can decrease the amount of loops:

#include <stdio.h>

int main(){
    int l,c,i,j;

    scanf("%d %d",&l,&c);

    char matriz[l][c];

    for(i = 0; i < l; i++){
        scanf("%s",matriz[i]);
    }

    for(i = 0; i < l; i++){
      for(j = 0; j < c; j++){
        if(matriz[i][j] == '.'){
          printf("A");
        } else {
          printf("%c", matriz[i][j]);
        }
      }
      printf("\n");
    }

    return 0;
}

See working here

Obs: scanf does not read to the end of the line and yes to a white-space (spaces, tab) or end of the line. If you want these characters in the matrix can be used %[^\n] instead of%s.

  • 1

    The scanf with %s does not read the whole line. It reads up to the first found blank space. I could explain this better since if the author assumes that the %s always reads to the end, he will have other problems.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.