程序不会终止 #包括 #包括 #包括 int main(){ 整数检验,i,j,k; scanf(“%d”、&num_测试); 字符数组[num_test][35]; 对于此代码块中的(i=0;i

程序不会终止 #包括 #包括 #包括 int main(){ 整数检验,i,j,k; scanf(“%d”、&num_测试); 字符数组[num_test][35]; 对于此代码块中的(i=0;i,c,arrays,string,while-loop,C,Arrays,String,While Loop,) #include<stdio.h> #include<string.h> #include<stdlib.h> int main(){ int num_test,i,j,k; scanf("%d ",&num_test); char array[num_test][35]; for (i=0;i<num_test;i++){ fgets(array[i],35,stdin);

#include<stdio.h>
#include<string.h>
#include<stdlib.h>


int main(){

    int num_test,i,j,k;
    scanf("%d ",&num_test);
    char array[num_test][35];
    for (i=0;i<num_test;i++){
        fgets(array[i],35,stdin);
        array[i][strcspn(array[i],"\n")]='\0';
    }
    for (j=0;j<num_test;j++){
        for (k=0;k<35;k++){
            while(array[j][k] != '\0'){        // I am seeing when the name entered ends then this loop should stop. 
                if (array[j][1] == 'a'){
                    array[j][1] = 'A';
                }
            }
        }           
    }
    return 0;
}

我还建议将“幻数”
35
替换为
\define
const int
while(array[j][k]!='\0')
=
if(array[j][k]='\0'){break;}
因为您已经在循环
k
。下面的行应该使用索引
k
而不是
1
,假设您希望每个
'a'
都大写。为什么您希望得到任何结果?您的代码没有输出任何内容。@ChrisTurner程序没有终止,它会继续接受输入no-它只接受e> num_test输入行。根据@WeatherVane的注释,它不会因为不必要的
while
循环而终止。事实上
while
循环一旦开始将永远不会终止,因为它的主体不会改变任何东西,从而导致它终止。
for (j=0;j<num_test;j++){
    for (k=0;k<35;k++){
        while(array[j][k] != '\0'){
            if (array[j][1] == 'a'){
                array[j][1] = 'A';
            }
        }
    }
}
for(j = 0; j < num_test; j++) {
    for(k = 0; k < 35; k++) {
        if(array[j][k] == '\0') {
            break;                      // quit when terminator found
        }
        if(array[j][k] == 'a') {        // changed 1 to k
            array[j][k] = 'A';          // changed 1 to k
        }
    }
}