Write an efficient C function that takes two strings as arguments and removes the characters from first string which are present in second string (mask string).
We strongly recommend that you click here and practice it, before moving on to the solution.
Algorithm: Let first input string be”test string” and the string which has characters to be removed from first string be “mask” 1: Initialize:
res_ind = 0 /* index to keep track of processing of each character in i/p string */
ip_ind = 0 /* index to keep track of processing of each character in the resultant string */
2: Construct count array from mask_str. Count array would be:
(We can use Boolean array here instead of int count array because we don’t need count, we need to know only if character is present in mask string)
count[‘a’] = 1
count[‘k’] = 1
count[‘m’] = 1
count[‘s’] = 1
3: Process each character of the input string and if count of that character is 0 then only add the character to the resultant string.
str = “tet tringng” // ’s’ has been removed because ’s’ was present in mask_str but we we have got two extra characters “ng”
ip_ind = 11
res_ind = 9