SpinWords

Write a function that takes in a string of one or more words, and returns the same string, but with all words that have five or more letters 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.

StringBuilder.reverse() String.join()

Test Sample is complex, we can process to beauty input

first try

public static String spinWords(String sentence) {  
    //TODO: Code stuff here  
    StringBuilder result = new StringBuilder();  
    StringBuilder word = new StringBuilder();  
    int count = 0;  
    for (int i = 0; i < sentence.length(); i++) {  
        char ch = sentence.charAt(i);  
        if (ch == ' ') {  
            if(count >= 5) {  
                result.append(reverse(word));  
            }            else {  
                result.append(word.toString());  
            }            result.append(" ");  
            word = new StringBuilder("");  
            count = 0;  
            continue;  
        }        word.append(ch);  
        count++;  
        if (i == sentence.length() - 1) {  
            if(count >= 5) {  
                result.append(reverse(word));  
            }            else result.append(word.toString());  
        }    }    return result.toString();  
}  
  
public static String reverse(StringBuilder word) {  
    StringBuilder result = new StringBuilder();  
    for (int i = word.length() - 1; i >= 0; i--) {  
        result.append(word.charAt(i));  
    }    return result.toString();  
}

That way too difficult. Actually, StringBuilder has method reverse

we can preprocess to separated words, and join in space.

public static String spinWords(String sentence)  {  
    //preprocess  
    String[] words = sentence.split(" ");  
    // what if replaced with enhanced-loop?  
    //  words[i] is directly modified because we assign a new string to the same index in the array.    //for(int i=0; i<words.length; i++) {  
        if (words[i].length() >= 5){  
            words[i] = new StringBuilder(words[i]).reverse().toString();  
        }    }    return String.join(" ", words);  
}
Last modified April 7, 2025: update codewar (7e82e67)