Write a program which takes two inputs one is a string and other is a character. The function should create a new string after deleting all occurrences of the character from the string and return the new string?
Answers
Answer:
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
4: Put a ‘\0′ at the end of the string?
del_char = lambda string, char: "".join([c for c in string if c != char])
string, char = input("string: "), input("char: ")
print(del_char(string, char))