Remove characters from input string that are present in mask string

Write a C program that takes two strings as arguments and removes the characters from first string which are present in second string called mask string.

ex: str1 “crazy for code”
str2 “cod”
Result: “razy e”

1) Get count array from mask string which stores count of chars from mask string.
2) Check for input string if it contains chars from count array with frequency > 0 then skip else copy that char in input string.
3) In the end, copy ‘\o’

int *getMaskStrCount(char *str)
	int *count = (int *)malloc(sizeof(int)*256);
	int i;
	for (i = 0; *(str+i);  i++)
	return count;

char *removeMaskStr(char *str, char *mask_str)
	int *count  = getMaskStrCount(mask_str);
	int i  = 0, j = 0;
	char temp;
	while(*(str + i))
		if(count[*(str + i)] == 0)
			*(str + j) = *(str + i);

	*(str+j) = '\0';

	return str;

Time Complexity: O(m+n) Where m is the length of mask string and n is the length of the input string.

One Thought on “Remove characters from input string that are present in mask string

  1. Tarun on June 27, 2015 at 7:42 pm said:

    public class CorrectQuestion {
    public static void main(String[] args) {
    String s1=”crazy for code” ;
    String s2=”cod”;
    char [] arr=new char[256];
    for(int i=0;i<s2.length();i++)
    String Str =" ";
    for(int i=0;i<s1.length();i++)

    if(arr[s1.charAt(i)] == 0)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Navigation