Development/CodeWorkout

[CodeWorkout] X125: repeatSeparator

bbubbush 2022. 4. 25. 00:10

Question

Given two strings, word and a separator sep, return a big string made of count occurrences of the word, separated by the separator string.

 

Answer

반응형
public class X125 {
  public static void main(String[] args) {
    String word = "";
    String sep = "";
    int count = 0;
    word = "Word";
    sep = "X";
    count = 3;
    System.out.printf("[word = %s, sep = %s, count = %d]\n", word, sep, count);
    System.out.println(repeatSeparator(word, sep, count));

    word = "catdog";
    sep = "K";
    count = 5;
    System.out.printf("[word = %s, sep = %s, count = %d]\n", word, sep, count);
    System.out.println(repeatSeparator(word, sep, count));
  }

  public static String repeatSeparator(String word, String sep, int count) {
    if (count == 0) {
      return "";
    }

    StringBuffer sb = new StringBuffer(word);
    for (int i = 1; i < count; i++) {
      sb.append(sep);
      sb.append(word);
    }
    return sb.toString();
  }
}

 

Ref

https://github.com/bbubbush/codeworkout/blob/master/src/com/bbubbush/tistory/X125.java

 

GitHub - bbubbush/codeworkout

Contribute to bbubbush/codeworkout development by creating an account on GitHub.

github.com

 

'Development > CodeWorkout' 카테고리의 다른 글

[CodeWorkout] X323: sumRange  (0) 2022.05.06
[CodeWorkout] X158: bunnyEars2  (0) 2022.05.05
[CodeWorkout] X119: squareUp  (0) 2022.04.27
[CodeWorkout] X121: extraEnd  (0) 2022.04.26