Stop gninnipS My sdroW!

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"

Good Solution1:

import java.util.Arrays;

public class SpinWords {

  public String spinWords(String sentence) {
    String[] words = sentence.split(" ");
    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);
  }
}

Good Solution2:

import java.util.stream.*;
import java.util.Arrays;

public class SpinWords {

  public String spinWords(String sentence) {
    return Arrays.stream(sentence.split(" "))
                 .map(i -> i.length() > 4 ? new StringBuilder(i).reverse().toString() : i)
                 .collect(Collectors.joining(" "));
  }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,173評論 0 23
  • 雨把太陽的熱情吻滅 風(fēng)聲唏噓 北國的冬天 飄起了雪 雪花一片一片 銜著我的思念 厚厚的絨被 蓋住整個季節(jié) 最恨癡情...
    哥窯閱讀 248評論 0 3
  • 六點半起床,練習(xí)keep半小時,洗漱,化妝,挑選衣服,發(fā)呆,一個人去食堂吃飯。小菜,小米粥,雞蛋餅,看周圍的人群,...
    小北之遇見一只喵兒閱讀 872評論 8 5
  • 作為職業(yè)背包客和微間歇年的踐行者,其實我還蠻不喜歡年輕人說走就走的旅行。 每年在路上,大多喜歡住在青旅,有更為自由...
    喬木以安閱讀 443評論 1 2

友情鏈接更多精彩內(nèi)容