Java Program to Remove Characters from the Input String which are Present in the Mask String

This is the Java Program to Remove Characters that belong to a Mask String from the Input String

Problem Description

Given a string of characters and a mask string remove all the characters of the mask string from the first string.
Example:

String x = “We belong to Russia”
Mask = “Wbos”

Output = “e elng t Ruia”

Problem Solution

Count the frequency of each character in the mask string in an array. Now, iterate through the user string and check each characters frequency in the frequency array. If it is zero, add that character to the output string.

Program/Source Code

Here is the source code of the Java Program to Remove Characters that belong to a Mask String from the Input String. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

advertisement
advertisement
  1.  
  2. // Java Program to Remove Characters that belong
  3. // to a Mask String from the Input String
  4.  
  5. import java.io.BufferedReader;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8.  
  9. public class RemoveMaskChar {
  10.     // Function to remove the mask characters
  11.     static String removeMaskCharacters(String str, String str2){
  12.         int[] array = new int[256];
  13.         int i;
  14.         for(i=0; i<str2.length(); i++){
  15.             array[str2.charAt(i)]++;
  16.         }
  17.         String output="";
  18.         for(i=0; i<str.length(); i++){
  19.             if(array[str.charAt(i)] == 0)
  20.                 output+=str.charAt(i);
  21.         }
  22.         return output;
  23.     }
  24.     // Function to read input and display the output
  25.     public static void main(String[] args) {
  26.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  27.         String str;
  28.         System.out.println("Enter the string");
  29.         try {
  30.             str = br.readLine();
  31.         } catch (IOException e) {
  32.             System.out.println("An I/O error occurred");
  33.             return;
  34.         }
  35.         String str2;
  36.         System.out.println("Enter the second string");
  37.         try {
  38.             str2 = br.readLine();
  39.         } catch (IOException e) {
  40.             System.out.println("An I/O error occurred");
  41.             return;
  42.         }
  43.         String output = removeMaskCharacters(str,str2);
  44.         System.out.println("Final String is " + output);
  45.     }
  46. }
Program Explanation

1. In function removeMaskCharacters(), an array is created to count the frequency of each character in the mask string.
2. The loop for(i=0; i<str2.length(); i++) is used to count the above mentioned frequencies.
3. The loop for(i=0; i<str.length(); i++) is used to iterate through the original string.
4. The condition if(array[str.charAt(i)] == 0) checks if the frequency of the current character in the array is equal to zero.
5. If it is zero, then the element is added to the output string.

Time Complexity: O(n) where n is the length of the string.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Runtime Test Cases
 
Case 1 (Simple Test Case):
 
Enter the string
We belong to Russia
Enter the second string
Wbos
Final String is e elng t Ruia
 
Case 2 (Simple Test Case - another example):
 
Enter the string
xyz
Enter the second string
xyz
Final String is

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.