‹ Back to the paper
Given are two strings, input string and a mask string that remove all the characters of the mask…
Given are two strings, input string and a mask string that remove all the characters of the mask string from the original string.
Example: INPUT: ORIGINALSTRING: communication
MASK STRING: mont
OUTPUT: cuicai
A class StringOp is defined as follows to perform above operation.
Some of the members of the class are given below:
Class name : StringOp
Data members/instance variables:
str : to store the original string
msk : to store the mask string
nstr : to store the resultant string
Methods / Member functions:
StringOp() : default constructor to initialize the data member with legal initial value
void accept( ) : to accept the original string str and the mask string msk in lower case
void form() : to form the new string nstr after removal of characters present in mask from the original string
void display( ) : to display the original string and the newly formed string nstr
Specify the class StringOp giving details of the constructor( ), void accept( ), void form() and void display( ). Define a main( ) function to create an object and call all the functions accordingly to enable the task.
Answer
Answer
Official answer keyimport java.util.Scanner;
class StringOp
{
String str; // original string
String msk; // mask string
String nstr; // resultant string
StringOp()
{
str = "";
msk = "";
nstr = "";
}
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the original string:");
str = sc.nextLine().toLowerCase();
System.out.println("Enter the mask string:");
msk = sc.nextLine().toLowerCase();
}
void form()
{
for (int i = 0; i < str.length(); i++)
{
char ch = str.charAt(i);
if (msk.indexOf(ch) == -1)
nstr = nstr + ch;
}
}
void display()
{
System.out.println("Original string : " + str);
System.out.println("New string : " + nstr);
}
public static void main(String args[])
{
StringOp ob = new StringOp();
ob.accept();
ob.form();
ob.display();
}
}Explanation: The constructor initialises str, msk and nstr to empty strings. accept() reads the original string and the mask string, converting both to lower case as required. form() scans every character of str and appends it to nstr only if it does not occur anywhere in msk (String.indexOf returns -1 when the character is absent), which effectively removes every character of msk from str. display() prints both strings. Tested for str="communication", msk="mont": the program correctly outputs nstr="cuicai" (verified by running the code), matching the example in the question.From ISC 2025 Specimen Computer Science Paper 1, question 6.