106k views
4 votes
Write a function named replaceSubstring. The function should accept three string object arguments entered by the user. We want to look at the first string, and every time we say the second string, we want to replace it with the third. For example, suppose the three arguments have the following values: 1: "the dog jumped over the fence" 2: "the" 3: "that" With these three arguments, the function would return a string object with the value "that dog jumped over that fence". Demonstrate the function in a complete program. That means you have to write the main that uses this.

1 Answer

7 votes

Answer:

public class Main{

public static void main(String[] args) {

System.out.println(replaceSubstring("the dog jumped over the fence", "the", "that"));

}

public static String replaceSubstring(String s1, String s2, String s3){

return s1.replace(s2, s3);

}

}

Step-by-step explanation:

*The code is in Java.

Create function called replaceSubstring that takes three parameters s1, s2, and s3

Use the replace function to replace the s2 with s3 in s1, then return the new string

In the main:

Call the replaceSubstring function with the given strings and print the result

User Maged Samaan
by
6.4k points