在上述问题中,如果使数组A保留字符串“ abcde”,那么上述四个测试用例中的哪一个将成功暴露此过程中的缺陷?
(A)无
(B)仅2
(C)仅3和4
(D)仅4答案: (C)
解释:
#include
#include
void find_and_replace(char *A, char *oldc, char *newc) {
for (int i = 0; i < 5; i++)
for (int j = 0; j < 3; j++)
if (A[i] == oldc[j]) A[i] = newc[j];
}
int main()
{
char *oldc1 = "abc", *newc1 = "dab";
char *oldc2 = "cde", *newc2 = "bcd";
char *oldc3 = "bca", *newc3 = "cda";
char *oldc4 = "abc", *newc4 = "bac";
char test[] = "abcde";
printf("Test 2\n");
printf("%s\n", test);
find_and_replace(test, oldc2, newc2);
printf ("%s\n", test);
printf("\nTest 3\n");
strcpy(test, "abcde");
printf("%s\n", test);
find_and_replace(test, oldc3, newc3);
printf ("%s\n", test);
printf("\nTest 4\n");
strcpy(test, "abcde");
printf("%s\n", test);
find_and_replace(test, oldc4, newc4);
printf ("%s\n", test);
}
输出:
Test 2
abcde
abbcd
Test 3
abcde
addde
Test 4
abcde
aacde
这个问题的测验