-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScrambleWord.java
More file actions
63 lines (50 loc) · 1.59 KB
/
ScrambleWord.java
File metadata and controls
63 lines (50 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.util.ArrayList;
/**
* Write a description of class ScrambleWord here.
*
* @author Sofie Budman
* Period 5 2/24/25
*/
public class ScrambleWord
{
public static String scrambleWord(String word)
{
String out = "";
for(int i = 0; i < word.length(); i ++){
if(i < word.length() -1 && word.substring(i,i+1).equals("A") && ! word.substring(i+1,i+2).equals("A")){
out += word.substring(i+1, i+2);
out += word.substring(i, i+1);
i++ ;
}
else{
out += word.substring(i, i+1);
}
}
return out;
}
//On exit: wordList should have all words unchanged (by scrambleWord) removed
public static void scrambleOrRemove(ArrayList<String> wordList)
{
for(int i = 0; i < wordList.size() ; i++){
if(scrambleWord(wordList.get(i)).equals( wordList.get(i))){
wordList.remove(i);
i--;
}else{
wordList.set(i, scrambleWord(wordList.get(i)));
}
}
}
public static void main(String[] argc)
{
ArrayList<String> arr = new ArrayList<String>();
arr.add("TAN");
arr.add("ABRACADABRA");
arr.add("WHOA");
arr.add("APPLE");
arr.add("EGGS");
//arr.add("AARDVARK");
ScrambleWord.scrambleOrRemove(arr);
System.out.println(arr);
System.out.println("Expected result: TNA BARCADABARA PAPLE");
}
}