in DS edited by
14,324 views
52 votes
52 votes

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”$

The tester now tests the program on all input strings of length five consisting of characters ‘$a$’, ‘$b$’, ‘$c$’, ‘$d$’ and ‘$e$’ with duplicates allowed. If the tester carries out this testing with the four test cases given above, how many test cases will be able to capture the flaw?

  1. Only one
  2. Only two
  3. Only three
  4. All four
in DS edited by
by
14.3k views

1 comment

1. the problem when  old char is replaced and then while checking we again check

it for like 2 time or 1 time more and so in way we are considering it as old

char  because code is flawed in that way.

2.So to catch this what should happen is you replace old char with some new   

char and then that should be again replaced with new char

3.And now it shouldn't repeat again otherwise it may nullify all the changes

which we detected till now, so be careful and check for it though that is   

not happening in given question
0
0

9 Answers

–3 votes
–3 votes

Here the flaw is not using 'break' statement after replacing the character at array A.

if (A[i] == oldc[j])
    A[i] = newc[j];break;

If we don't use break then ,the updated character will be again replaced if it is present anywhere at oldc[j+1] ,oldc[j+2]......oldc[2]  

reshown by
Answer:

Related questions