c语言改错
1.下面是一段关于字符串程序,请改正程序中的错误。
#include <stdio.h>
main()
{
char str2[6]=”Yes,it is”;_______①________
char str3[10];
scanf(“%c”,&str3);_______②_________
strcpy(“Yes,it is”,str3);_______③_________
if(str2= =str3)_______ ④_______
printf(“%s”,str3[ ]);________ ⑤_________
else
{
printf(“%s”,str3[ ]);________ ⑤__________
printf(“%s”,str2[5]);________ ⑥ __________
}
return(1);
}
2.程序用于求矩阵的主对角线元素之和。
#include <stdio.h>
main()
{
int a[3][ ]={{1,2,4},{},{5,7}};______①_______
int b[3][3];
int sum1=sum2=0;
int j,k;
for(j=0;j<=2;j++)
{
for(k=0;k<=2;k++)
scanf(“%d”,b[j][k]);_______②________
}
for(k=1;k<=3;k++)________③________
{
sum1+=a[k][k];
sum2+=b[k][k];
}
printf(“%d,%d”,&sum1,&sum2);_______④_________
}
① 下标长度不对,可改为 char str2[]="Yes,it is";
②格式类型不对,改为scanf(“%s”,&str3);
③ 参数写反了,改为 strcpy(str3,“Yes,it is");
④字串不能直接比较,要用strcmp(str2,str3);
⑤,⑤,⑥都是下标错(把下标去掉就可以了)
另外,从程序功能上讲,你的②或③的其中一个应该是str2(你两个都是str3了)