[英]scan strings for 100x100 numbers in c programming
I have been searching all google or stackoverflow, but could not found it. 我一直在搜索所有的google或stackoverflow,但找不到它。 :( :(
I have 100 strings, each string is a number with length = 100, the strings are seperated by a break_line. 我有100个字符串,每个字符串都是一个长度= 100的数字,这些字符串由break_line分隔。 Example of input: 输入示例:
010011001100..... (100 numbers)
...(98 strings)
0011010101010.... (100 numbers)
the ouput should be an array A[100][100] for each single number from the strings. 对于字符串中的每个数字,输出应为数组A [100] [100]。
My codes do not work, could you please help to correct it: 我的代码不起作用,请您帮忙更正它:
#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");
}
}
Thank you so much. 非常感谢。 ! !
Your code has two problems: 您的代码有两个问题:
#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");
}
}
It should be 它应该是
#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");
}
}
or 要么
#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");
}
}
I got the answer for my problem from Mr./Ms. 我从先生/女士那里得到了我的问题的答案 。 BLUEPIXY . BLUEPIXY 。
It is 它是
scanf("%1d", &b[i][j]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.