-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stop gninnipS My sdroW.java
37 lines (31 loc) · 1.21 KB
/
Stop gninnipS My sdroW.java
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
/*
Write a function that takes in a string of one or more words, and returns the same string,
but with all five or more letter words reversed (Just like the name of this Kata).
Strings passed in will consist of only letters and spaces. Spaces will be included only when more than
one word is present.
Examples: spinWords( "Hey fellow warriors" ) => returns "Hey wollef sroirraw" spinWords( "This is a test")
=> returns "This is a test" spinWords( "This is another test" )=> returns "This is rehtona test"
*/
public class SpinWords {
public static String spinWords(String sentence) {
//TODO: Code stuff here
String[] newStringArray = sentence.split(" ");
StringBuilder sb= new StringBuilder();
for (int i = 0; i < newStringArray.length; i++) {
sb.append(newStringArray[i]);
if(newStringArray[i].length()>=5) {
newStringArray[i]= sb.reverse().toString();
}
sb.setLength(0);
}
String finalString = "";
for (int i = 0; i < newStringArray.length; i++) {
if(i==newStringArray.length-1) {
finalString += newStringArray[i];
}else {
finalString += newStringArray[i]+" ";
}
}
return finalString;
}
}