在 C 编程中扫描 100x100 数字的字符串

scan strings for 100x100 numbers in c programming

我一直在搜索所有 google 或 Whosebug,但找不到。 :(

我有 100 个字符串,每个字符串都是一个长度为 100 的数字,字符串之间用 break_line 分隔。 输入示例:

010011001100..... (100 numbers)
...(98 strings)
0011010101010.... (100 numbers)

对于字符串中的每个数字,输出应该是一个数组 A[100][100]。

我的代码不起作用,请你帮忙更正一下:

#include <stdio.h>

char a[100][100];
int b[100][100];
int i,j;


int main(void)
{

    for(i = 0; i < 100; i ++){
        for(j = 0; j < 100; j ++){
            scanf ("%s", a[i][j]);
            b[i][j] = a[i][j] - '0';
            printf("%d", b[i][j]);
        }
        printf("\n");
    }
}

非常感谢。 !

您的代码有两个问题:

#include <stdio.h>

char a[100][100]; /* No space for the NUL-terminator */
int b[100][100];
int i,j;


int main(void)
{

    for(i = 0; i < 100; i ++){
        for(j = 0; j < 100; j ++){
            scanf ("%s", a[i][j]); /* %s expects a char*, not a char */
            b[i][j] = a[i][j] - '0';
            printf("%d", b[i][j]);
        }
        printf("\n");
    }
}

应该是

#include <stdio.h>

char a[100][101]; /* Note the 101 instead of 100 */
int b[100][100];
int i,j;


int main(void)
{

    for(i = 0; i < 100; i ++){
        scanf ("%s", a[i]); /* Scan a string */
        for(j = 0; j < 100; j++){
            b[i][j] = a[i][j] - '0';
            printf("%d", b[i][j]);
        }
        printf("\n");
    }
}

#include <stdio.h>

char a[100][100]; /* No need for space for the NUL-terminator as %s isn't used */
int b[100][100];
int i,j;


int main(void)
{

    for(i = 0; i < 100; i ++){
        for(j = 0; j < 100; j ++){
            scanf (" %c", &a[i][j]); /* Scan one character, space before %c ignores whitespace characters like '\n' */
            b[i][j] = a[i][j] - '0';
            printf("%d", b[i][j]);
        }
        printf("\n");
    }
}

我得到了 from Mr./Ms. BLUEPIXY

scanf("%1d", &b[i][j]);