Problem 1
/*
- Write a program that will remove all occurrences of the second string from the first string.
*/
// Import
import java.util.Scanner;
// Main class
public class RemoveString
{
// Method to remove occurrence of string from another string
public static void removeOccurrence(String str, String str_toRemove)
{
// Converting string to string array
String[] str_arr = str.split(" ");
// Iterating over array elements
for (int i = 0; i < str_arr.length; i++)
{
//Ccheck for occurrence of string to be removed
//and remove it
if (str_arr[i].equals(str_toRemove))
{
str_arr[i] = "";
} //end of if-block
else
{
continue;
}//end of else-block
//end of for loop
}
// printing modified string
System.out.println("\nMODIFIED STRING :");
// For-each loop
for (String s : str_arr)
{
System.out.print(s + " ");
}// End of For-each loop
System.out.println();
}//Method end
//Main method
public static void main(String[] args)
{
// Object of the Scanner class
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string/paragraph : ");
// Reading a string from the user
String long_str = sc.nextLine();
System.out.print("Enter a string/word : ");
// Reading a string from the user
String short_str = sc.nextLine();
System.out.println("Original string : \n" + long_str);
// Calling method
RemoveString.removeOccurrence(long_str, short_str);
/* Author @Ayesha Sumayya Khanum */
// End of main
}
// Class exit
}