我是 C 的初学者。我试图在 Code::Blocks 和 Ideone 中运行这段代码,并且都给了我相同的运行时错误。我尝试更改arr[100]
为arr[101]
,令人惊讶的是,代码运行了。我也担心 的初始化poo[t]
,因为它似乎没有在其中存储 的值sum
。或者可能sum
保留值0
(可能是该switch
语句不允许char
案例吗?)
#include <stdio.h>
#include <stdlib.h>
int main()
{
int arr[100], t, poo[t], i, j, sum; // refer HOLES
scanf("%d", &t);
for (i=0; i<t; i++) {
scanf("%s", arr);
j=0;
sum = 0;
while (arr[j] != '\0') {
switch (arr[j])
{
case 'B':
sum = sum + 2;
break;
case 'A':
case 'D':
case 'O':
case 'P':
case 'Q':
case 'R':
sum++;
break;
default:
break;
}
j++;
}
poo[i] = sum;
printf("%d", poo[i]);
}
for (i=0; i<t; i++)
{
printf("%d \n", poo[i]);
}
return 0;
}