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”

Steps:
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++)
		count[*(str+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);
			j++;
		}
		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.

2 Thoughts 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];
    System.out.println();
    for(int i=0;i<s2.length();i++)
    {
    arr[s2.charAt(i)]++;
    }
    String Str =" ";
    for(int i=0;i<s1.length();i++)
    {

    if(arr[s1.charAt(i)] == 0)
    {
    Str+=s1.charAt(i);
    }
    }
    System.out.println(Str.trim());
    }
    }

  2. package com.string;

    import java.util.Iterator;
    import java.util.LinkedHashSet;
    import java.util.Set;

    public class RemoveCharacterFromFisrtStringWhichPInSecondString {

    public static void removeCharactersFromFirstString(String str1,String str2){

    StringBuilder sb = new StringBuilder(str1.toLowerCase());

    char maskArray[] = populateMaskArray(str2);

    for(int i=0;i<str1.length();i++){

    char ch = str1.toLowerCase().charAt(i);
    int index = sb.toString().indexOf(ch);
    if(maskArray[ch] == ch) {
    sb.deleteCharAt(index);
    }
    }
    System.out.println(sb.toString());
    }

    /**
    * Time Complexity: O(m) Where m is the length of mask string.
    */
    static char[] populateMaskArray(String mask) {
    char[] array = new char[256];
    for(int i = 0; i < mask.length(); i++) {
    array[mask.charAt(i)] = mask.charAt(i);
    }
    return array;
    }

    public static void main(String[] args) {

    String str1 = "India is country";
    String str2 = "in";

    removeCharactersFromFirstString(str1,str2);

    }

    }

Leave a Reply to Tarun Cancel 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