美文网首页
Java practice recording 2020-05-

Java practice recording 2020-05-

作者: 修玛哦 | 来源:发表于2020-05-02 02:24 被阅读0次

    Java practice recording 2020-05-01

    Given a string, return a string made of the chars at indexes 0,1, 4,5, 8,9 ... so "kittens" yields "kien".

    altPairs("kitten") → "kien"
    altPairs("Chocolate") → "Chole"
    altPairs("CodingHorror") → "Congrr"

    public String altPairs(String str) {
      String result = "";
      
      // Run i by 4 to hit 0, 4, 8, ...
      for (int i=0; i<str.length(); i += 4) {
        // Append the chars between i and i+2
        int end = i + 2;
        if (end > str.length()) {
          end = str.length();
        }
        result = result + str.substring(i, end);
      }
      
      return result;
    }
    

    相关文章

      网友评论

          本文标题:Java practice recording 2020-05-

          本文链接:https://www.haomeiwen.com/subject/mddhghtx.html