这样为什么会得到正确输出
程序代码:
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> int main() { int i,m=0,n=0; char c1, c2; char str1[80], str2[20]; printf("Input str1:\n"); gets_s(str1); printf("The str1 is:\n"); for (i = 0; (c1 = str1[i]) != '\0'; i++) { m++; printf("%c", str1[i]); } printf("\n"); printf("m=%d\n", m); printf("Input str2:\n"); gets_s(str2); printf("The str2 is:\n"); for (i = 0; (c2 = str2[i]) != '\0'; i++) { n++; printf("%c", str2[i]); } printf("\n"); printf("n=%d\n",n); if (n <= 80 - (m + 1)) { printf("Now we connect str1 and str2:\n"); for (i = 0; i < m + n; i++) { c1 = str1[i]; c2 = str2[i]; str1[m + i] = c2; printf("%c", str1[i]); } } else printf("There is an error in your input,please try again!"); printf("\n"); return 0; }
最后面这个
for (i = 0; i < m + n; i++)
{
c1 = str1[i];
c2 = str2[i];
str1[m + i] = c2;
printf("%c", str1[i]);
}
str2[i]明显超了,为什么输出是对的
[此贴子已经被作者于2016-12-28 10:02编辑过]