Write a Java function such that Given two strings, word and a separator, return a big string made of count occurences of the word, separated by the separator string.
repeatSeparator("Word", "X", 3) → "WordXWordXWord"
repeatSeparator("This", "And", 2) → "ThisAndThis"
repeatSeparator("This", "And", 1) → "This"
My code is as below but is not working
public String repeatSeparator(String word, String sep, int count) {
if(count == 1) {
return word;
}
if(count > 1) {
for (int i = 0; i < count-1; i++){
word = word + sep + word;
}
}
return word;
}
Example Output ::
Expected Run Result
repeatSeparator("Word", "X", 3) → "WordXWordXWord" "WordXWordXWordXWord" X