In: Computer Science
JAVA WITH NO IMPORTS
Method changeWords executes a find and replace like you would
with ctrl+f
or command+f on
here
The first value
provided is the text that you search
Turn the text
into an array of strings
find the string
that is sent in the second position (find) and
replace it with
the
changeWords("never
have I ever", "ever", "cheated") returns ["never", "have", "I",
"cheated"]
changeWords("Why
why Why", "Why", "because") returns ["becasue", "because",
"because"]
changeWords("What is up bro", "hi", "hello") returns ["what", "is",
"up", "bro"]
changeWords("Hi
nice to meet you", "hi", "hello") returns ["hello", "nice", "to",
"meet", "you"]
changeWords( "",
"", "") returns []
@param String text,
String of multiple words
@param String
find, the string to be replaced
@param String
replace, the string to be inserted in place of find
@return String array,
where words is the array of words after the edit is made
*/
public static String[] changeWords(String text, String find, String
replace)
{
//your code here
String[] words = new
String[0];//you are free to edit this line
return words;
}//end changeWords
import java.util.Arrays;
public class StringReplace {
public static void main(String[] args) {
String arr[]=changeWords("never have I ever", "ever", "cheated");
String arr2[]=changeWords("Why why Why", "Why", "because");
for(String x:arr)
System.out.print(x+" ");
System.out.println();
for(String x:arr2)
System.out.print(x+" ");
}
public static String[] changeWords(String text, String find, String replace)
{
// splitting the string into words
String words[] = text.split(" ");
// iterating through the words
for(int i=0;i<words.length;i++) {
//checking if we have find than replcae it
if(words[i].equalsIgnoreCase(find))
words[i]=replace;
}
return words;
}
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
Please Like and Support me as it helps me a lot