2

I'm supposed to write a simple method that returns given string for given amount, seperated by comma (and no comma in the end), with recursion. If there are less than two counts, the return is empty string "".

final static String COMMA = ", ";
public static String replicate(String s, int count) {
    String answer = "";
    if (count < 2) {
        return answer;
    }
    else {
        answer = s + COMMA + replicate(s, (count - 1));
        return answer;
    }
}

If I put s = cat and count = 5, I get cat, cat, cat, cat,- One short what I need. I'm at my wits end here what to do, to get proper amount of repeats here without the comma at the end.

EDIT: Clearly I failed to communicate, that the method SHOULD return an empty string, if the count is two or less. Sorry for the lack of clarity there.

1
  • 1
    Try String answer = s;... Commented Dec 9, 2019 at 16:16

3 Answers 3

1

You're extremely close! When you hit your base case count < 2, instead of returning an empty string, you can return your input string. Be sure to check that length isn't 0, too.

EDIT: from information in the comments, you want to return an empty string for any counts less than or equal to two. Odd requirement, but this will fit that issue; it uses three as a base case instead of two, returning three s inputs concatenated together.

  final static String COMMA = ", ";
  public static String replicate(String s, int count) {
      String answer = "";
      if(count <= 2) {
        return answer;
      }
      if(count == 3) {
        return (s + COMMA + s + COMMA + s);
      }
      else {
          answer = s + COMMA + replicate(s, (count - 1));
          return answer;
      }
  }

Demo

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your answer! I failed to say that the method should return the empty string in case of two or less count - this answer returns still the given string with count one.
Something must've been lost in translation then. What should the return values be for replicate("cat", 2) and replicate("cat", 1)?
For String repeat = replicate("cat", 1) = ""; and String repeat = replicate("cat", 2); repeat = "cat, cat";
Weird requirement, but okay! Answer and demo updated.
Thank you very much! I was thrown off by that requirement also, but playing around the base seemed to work evidently.
|
0

Every element is there, just a bit unreadable code.

  • If count is 0, "".
  • If one, s itself.
  • Otherwise s, + recursive result.

So:

public static String replicate(String s, int count) {
    if (count <= 0) {
        return "";
    } else if (count == 1) {
        return s;
    } else {
        return s + COMMA + replicate(s, count - 1);
    }
}

What lead to mental failure was the variable answer, and the two cases of < 2.

Comments

0
public static String replicate(String s, int count) {

    if (count >= 2) {
        s = s + COMMA + replicate(s, --count);
    }
    return s;
}

replicate("hii", 4) --> return "hii,hii, hii, hii"

COMMA + replicate("hii", 3) --> return "hii, hii, hii" COMMA + replicate("hii", 2) --> return "hii, hii" COMMA + replicate("hii", 1) --> return "hii"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.