1.5k views

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 = “dab”$
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?

1. None
2. $2$ only
3. $3$ and $4$ only
4. $4$ only
in DS
edited | 1.5k views

The test cases 3 and 4 are the only cases that capture the flaw. The code does not work properly when an old character is replaced by a new character and the new character is again replaced by another new character. This does not happen in test cases (1) and (2), it happens only in cases (3) and (4).

by Boss
selected by Here when the element of array $A$ and $oldc$ match , we replace that array element of $A$  with  array element of $newc$ . For every element of $A$ array update occurs maximum one time.

Similarly for (2) array element of $A$ has updated with array element of $newc$ less than or equal to one time, Now, for (3) when $i=0$ , value of $A$ match with $oldc$ i.e.'$a$' , and replace with $newc$ i.e. also '$a$'. So, no changes

when $i=1$ value of array $A=$'$b$'

match with $oldc=$'$b$' and replace with $newc=$'$c$'.

Now, $A=$'$c$' which equal with next element of $oldc=$'$c$'.

So, replace again with $newc=$'$d$'.

Now, we can say here in array $A$ value replace with $newc$ value , and that $newc$ value replace with next $newc$ value. Similarly for (4) here $2$ times replacement for $A$ with element $newc$ and $newc$

Updating of $newc$ value with another $newc$ value is calling flaw here

So Ans (C)

by Veteran
edited by
0

srestha plz explain the meaning of "exposing the flaw"

0
i m neither getting question nor answer , plz @bikram sir help!!!
+10
the logic is simple- If the updated value matches again with any of the character in the the oldc array, it will be updated again and that's the flaw we are talking about.