Question

In: Computer Science

Write the method: public static String removeSubstring(String s1, String s2) Method removeSubstring(String s1, String s2) Must...

Write the method: public static String removeSubstring(String s1, String s2) Method removeSubstring(String s1, String s2)

Must do 3 things:
1. Take two parameters, the original String (String s1) and the substring (String s2) that is to be removed;
2. Create a new String that consists of the original String data less all occurrences of the substring data;
3. Return the new String.

Note:
1. The data to be removed is each occurrence of the complete substring, not individual characters of the substring. For example, if the original String is “hello” and the substring is “el”, the String to be returned is “hlo” and not “ho”.
2. The second, third and fourth examples include spaces, so the substring also includes a space that is to be removed.

PLEASE HELP ME UNDERSTAND HOW TO COMPLETE THIS CODE BELOW:

public class RemoveSubstring
{
   public static void main(String [] args)
   {

   String testString1 = "173167176531317";
   String subStr1 = "17";
  
   String testString2 = "I am not happy";
   String subStr2 = "not ";
  
   String testString3 = "I am not happy and not glad";
   String subStr3 = "not ";
  
   String testString4 = "neither this nor that works!";
   String subStr4 = "neither this nor ";
  
   System.out.println("The string " + testString1 + " without " + subStr1 + " is: " + removeSubstring(testString1, subStr1));
   System.out.println("The string " + testString2 + " without " + subStr2 + " is: " + removeSubstring(testString2, subStr2));
   System.out.println("The string " + testString3 + " without " + subStr3 + " is: " + removeSubstring(testString3, subStr3));
   System.out.println("The string " + testString4 + " without " + subStr4 + " is: " + removeSubstring(testString4, subStr4));

   }

   public static String removeSubstring(String s1, String s2)
   {
  
return("PLEASE HELP ME UNDERSTAND HOW THIS WORKS OR WHERE TO LEARN MORE I AM LOST HERE THANK YOU");
   }

}

Solutions

Expert Solution

Function is thoroughly explained below in code comments. For your Better understanding i have also provided a screenshot of code in IDE.full program code is also given alongwith output screenshot. If need any further clarification please ask in comments.

#################################################################################

FUNCTION WITH EXPLANATION

public static String removeSubstring(String s1,String s2)
        {
                String result=""; //string to store our final result
                for(int i=0;i<s1.length();i++) //loop through every character of string
                {
                        if(s1.charAt(i)==s2.charAt(0)) //if the current character of string is equal to first character of substring
                        {
                                int j=0;
                                for(j=0;j<s2.length();j++) //loop through all characters of substring
                                {
                                        if(s1.charAt(i+j)!=s2.charAt(j)) //if the character in string does not match with character in substring break the loop
                                                break;
                                }
                                if(j==s2.length()) //if the j is equal to size of s2, that means there were all the characters of substring s2 in string s1 
                                {
                                        i=i+j-1; //increase the i until last element upto which substring is present in string
                                        continue; //continue the loop because we dont want to store those charcters which match with substring
                                }
                        } //if condition closed
                        
                        result=result+s1.charAt(i); //if the character does not match then add in result 
                } //for loop closed
                
                return result;
        }

SCREENSHOT IN IDE FOR BETTER UNDERSTANDING

##########################################################################

FULL CODE


public class RemoveSubstring
{
   //method to remove all occurence of a substring from string 
        public static String removeSubstring(String s1,String s2)
        {
                String result=""; //string to store our final result
                for(int i=0;i<s1.length();i++) //loop through every character of string
                {
                        if(s1.charAt(i)==s2.charAt(0)) //if the current character of string is equal to first character of substring
                        {
                                int j=0;
                                for(j=0;j<s2.length();j++) //loop through all characters of substring
                                {
                                        if(s1.charAt(i+j)!=s2.charAt(j)) //if the character in string does not match with character in substring break the loop
                                                break;
                                }
                                if(j==s2.length()) //if the j is equal to size of s2, that means there were all the characters of substring s2 in string s1 
                                {
                                        i=i+j-1; //increase the i until last element upto which substring is present in string
                                        continue; //continue the loop because we dont want to store those charcters which match with substring
                                }
                        } //if condition closed
                        
                        result=result+s1.charAt(i); //if the character does not match then add in result 
                } //for loop closed
                
                return result;
        }
        
        public static void main(String [] args)
   {

   String testString1 = "173167176531317";
   String subStr1 = "17";
  
   String testString2 = "I am not happy";
   String subStr2 = "not ";
  
   String testString3 = "I am not happy and not glad";
   String subStr3 = "not ";
  
   String testString4 = "neither this nor that works!";
   String subStr4 = "neither this nor ";
  
   System.out.println("The string " + testString1 + " without " + subStr1 + " is: " + removeSubstring(testString1, subStr1));
   System.out.println("The string " + testString2 + " without " + subStr2 + " is: " + removeSubstring(testString2, subStr2));
   System.out.println("The string " + testString3 + " without " + subStr3 + " is: " + removeSubstring(testString3, subStr3));
   System.out.println("The string " + testString4 + " without " + subStr4 + " is: " + removeSubstring(testString4, subStr4));

   }
}

##########################################################################

OUTPUT


Related Solutions

Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings...
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings as parameters and returns true if the two strings contain the same sequence of characters as each other but in the opposite order and false otherwise. • The recursive function should ignore capitalization. (For example, the call of isReverse("hello", "eLLoH") would return true.) • The empty string, as well as any one letter string, should be its own reverse. Write a driver program that...
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { //...
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str). May an array of...
Consider this program: public class Main { public static void main(String[] args) { String s1 =...
Consider this program: public class Main { public static void main(String[] args) { String s1 = "hello"; String s2 = "hello"; String s3 = new String("hello"); System.out.println(s1 == s2); System.out.println(s2 == s3); System.out.println(s2.equals(s3)); } } When we run the program, the output is: true false true Explain why this is the output, using words and/or pictures.
(10 marks) Write a function to check whether string s1 is a substring of string s2....
Write a function to check whether string s1 is a substring of string s2. The function returns the first index in s2 if there is a match. Otherwise, return -1. For example, the function should return 2 if s1 is "to" and s2 is "October". If s1 is "andy" and s2 is "candy", then the function should return 1. The function prototype is as follows: int indexOf(const char *s1, const char *s2).
How do you write a Java method that is called : public static String[] noIdenticalCombine(String[] array1,...
How do you write a Java method that is called : public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str)....
How to write Method in Java: public static in firstLetterFreq(char letter, String[] words] { // it...
How to write Method in Java: public static in firstLetterFreq(char letter, String[] words] { // it returns 0 if (words == null OR words.length == 0) // returns number of times letter is the first letter of a String in words array // use equalIgnoreCase to check if letter and str.charAt(0) are equal and ignoring case }
1.   What is the output of the following code: string s1 = “X”; string s2 =...
1.   What is the output of the following code: string s1 = “X”; string s2 = “A” + s1 + “BC” + s1 + “DEF” + s1 + “G”; cout << s2; 2.   What is the output of the following code: string s1 = “X”; string s2 = “A” + s1 + “BC” + s1 + “DEF” + s1 + “G”; cout << s[0] + s[3]; 3.   What is the output of the following code: string s1 = “X”; string...
In C: Find a string within a string Given two strings S1 & S2, search for...
In C: Find a string within a string Given two strings S1 & S2, search for an occurrence of the second string within a first string. Note: Do not use system library for the implementation. Input: Code Zinger University Zinger where, First line represents string S1. Second line represents string S2. Output: 5 Here 'Zinger' word starts at 5th index within 'Code Zinger University’. Assume that, The length of strings S1 & S2 are within the range [1 to 10000]....
Given the following method declaration, write a valid method call. public static void calcArea(String roomName, int...
Given the following method declaration, write a valid method call. public static void calcArea(String roomName, int length, int width)
Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine...
Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine if a given string is a palindrome. [A palindrome is a string that reads the same forwards and backwards, for example ‘racecar’, ‘civic’]. Make sure that your method works correctly for special cases, if any. (b) What is the big-O complexity of your method in terms of the list size n.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT