Direction: The procedure given below is required to find and replace certain characters inside an input character string supplied in array A. The characters to be replaced are supplied in array oldc, while their respective replacement characters are supplied in array newc. Array A has a fixed length of five characters, while arrays oldc and newc contain three characters each. However, the procedure is flawed.
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];
}
The procedure is tested with the following four test cases.
1. oldc = “abc”, newc = “da b”
2. oldc = “cde”, newc = “bcd”
3. oldc = “bca”, newc = “cda”
4. oldc = “abc”, newc = “bac”
-
If array A is made to hold the string “abcde”, which of the above four test cases will be successful in exposing the flaw in this procedure?
-
- 2 only
- 4 only
- 3 and 4 only
- None
Correct Option: B
Here, when the element of array A and old C match, we replace that array element of A with array element of new C for every element of A array update occurs maximum one time. Similarly for (2) array element of A has updated with array element of new C less than or equal to one time.
Now, for (3), when i = O, value of A which old C[2] i.e. ‘a’ and replace with new C[2] i.e. also ‘a’, So, no changes. when i = 1 value of array. A[1] = ‘b’
Match with Old C [O] = ‘b’ and replace with new C [O] = ‘C’.
Now, A [1] = ‘C’, which equal with ‘d’. Next element of old C [1] = ‘C’.
So, replace again with new C [1] = ‘d’.
Now, we can say here in Array A [1] value replace with new C [O] value, and that new C [O] value replace with next new C [1] value.
Similarly for (4) here 2 times replacement for A[O] with element new C [O] and new C [1]. Updating of new C value with another new C value is calling flow here, Hence the option (c) is correct.