Open In App

Algorithms | Misc | Question 11

In the above question, 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?
(A) None
(B) 2 only
(C) 3 and 4 only
(D) 4 only

Answer: (C)
Explanation:




#include <stdio.h>
#include <string.h>
  
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);
}

Output:

Test 2
abcde
abbcd

Test 3
abcde
addde

Test 4
abcde
aacde

Quiz of this Question


Article Tags :