In: Computer Science
***USE JAVA AND NO IMPORTS***
Method editString takes a string and breaks it into an array of
single
words which are then are edited
based on the command
Possible Commands:
remove: for the sent words, remove
those from the text
replace: replace the word in an
even element of words with a word in an odd element of words
add: add the word given word in the
index indicated after the word
editString("What is up bro",
"remove", ["bro"]) returns ["What", "is", "up"]
editString("What is up bro",
"replace", ["bro", "brother"]) returns ["What", "is", "up",
"brother"]
editString("What is up", "add",
["my", "3", "friend", "4"]) returns ["What", "is", "up", "my",
"friend"]
editString("I can only do so much",
"remove", ["only", "so"]) returns ["I", "can", "do", "much"]
editString("Tis but a flesh wound",
"replace", ["flesh", "scratch", "wound", "mark"]) returns ["Tis",
"but", "a", "scratch", "mark"]
editString("I can code in java",
"add", ["html", "4", "and", 5]) returns ["I", "can", "code", "in",
"html", "and", "java"]
@param text String of words
@return an array of lowercased
words in alphabetical order
*/
public static ArrayList<String>
editString(String text, String command, ArrayList<String>
words)
{
ArrayList<String> edited =
new ArrayList<String>();
return edited;
}//end editString
Please look at my code and in case of indentation issues check the screenshots.
---------------solution.java----------------
import java.util.*; // import the ArrayList, Scanner class
public class solution
{
public static ArrayList<String>
editString(String text, String command, ArrayList<String>
words){
ArrayList<String> edited =
new ArrayList<String>();
List<String> list =
Arrays.asList(text.split(" ")); //convert text to
List
for(String w: list){
//convert List to ArrayList
edited.add(w);
}
if (command ==
"remove"){ //when
command is remove
for(String w:
words){ //for
each word in words, remove it from edited ArrayList
edited.remove(w);
}
}
else if(command ==
"replace"){ //when command is
replace
String from,
to;
for(int i = 0; i
< words.size(); i = i+2){ //loop in words
ArrayList
from = words.get(i);
//get from word and to word
to = words.get(i+1);
for(int j = 0; j < edited.size();
j++){ //loop in edited ArrayList
if(edited.get(j).equals(from)) //if String is from
word, then change into to word
edited.set(j, to);
}
}
}
else if(command ==
"add"){ //when command is add
int index;
for(int i = 0; i
< words.size(); i = i+2){ //loop
for each word in words
index =
Integer.valueOf(words.get(i+1)); //get the index where
we must add it
edited.add(index, words.get(i));
//add the word at the given
index
}
}
return edited;
}
public static void printList(ArrayList<String>
list){
System.out.println("The list is:
");
for(String w: list){
System.out.print(w + " ");
}
System.out.println("\n");
}
public static void main(String[] args)
{
//for testing the method
ArrayList<String> edited =
new ArrayList<String>();
ArrayList<String> words = new
ArrayList<String>();
System.out.println("editString(\"What is up bro\", \"remove\",
[\"bro\"])");
//test case 1
words = new
ArrayList<String>(Arrays.asList("bro"));
edited = editString("What is up
bro", "remove", words);
printList(edited);
System.out.println("editString(\"What is up bro\", \"replace\",
[\"bro\", \"brother\"])"); //test case 2
words = new
ArrayList<String>(Arrays.asList("bro", "brother"));
edited = editString("What is up
bro", "replace", words);
printList(edited);
System.out.println("editString(\"What is up\", \"add\", [\"my\",
\"3\", \"friend\", \"4\"])"); //test case 3
words = new
ArrayList<String>(Arrays.asList("my", "3", "friend",
"4"));
edited = editString("What is up",
"add", words);
printList(edited);
System.out.println("editString(\"I can only do so much\",
\"remove\", [\"only\", \"so\"])"); //test case 4
words = new
ArrayList<String>(Arrays.asList("only", "so"));
edited = editString("I can only do
so much", "remove", words);
printList(edited);
System.out.println("editString(\"Tis but a flesh wound\",
\"replace\", [\"flesh\", \"scratch\", \"wound\",
\"mark\"])");
words = new
ArrayList<String>(Arrays.asList("flesh", "scratch", "wound",
"mark")); //test case 5
edited = editString("Tis but a
flesh wound", "replace", words);
printList(edited);
System.out.println("editString(\"I can code in java\", \"add\",
[\"html\", \"4\", \"and\", \"5\"])"); //test case 6
words = new
ArrayList<String>(Arrays.asList("html", "4", "and",
"5"));
edited = editString("I can code in
java", "add", words);
printList(edited);
}
}
--------------Screenshots--------------------
------------------Output------------------
---------------------------------------------------------------------------------------------------------
Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs
down.
Please Do comment if you need any clarification.
I will surely help you.