1

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    
1
  • 1
    And your question is...? Commented Dec 20, 2010 at 18:48

13 Answers 13

3

word = word + sep + word;

Think carefully about what this does the second time around. Hint: word has changed since the first time around.

Solution: Use a different variable to hold the results, so that you append the same word each time. (Free hint: use a StringBuffer or StringBuilder instead.)

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

Comments

2

The below function should do what you need:

public String repeatSeparator(String word, String sep, int count) {    
    StringBuffer buffer = new StringBuffer();

    while (count > 0) {
        buffer.append(word);
        count--;
        if (count > 0) {
            buffer.append(sep);
        }
    }

    return buffer.toString();                           
}

Comments

1
public string doStuff(
    final String word,
    final String seperator,
    final int count)
{
    StringBuffer buffer = new StringBuffer();
    for (int index = 0; index < count; ++index)
    {
        if (buffer.length() > 0)
        {
            buffer.append(seperator);
        }

        buffer.append(word);
    }

    return buffer.toString();
}

Comments

1
public String repeatSeparator(String word, String sep, int count) {        
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < count; i++) {
    sb.append(word);
    if (i < count - 1)
        sb.append(sep);
    }
    return sb.toString();
}

In real life, would use Apache Common Lang StringUtils.repeat

Comments

0

Don't for get to consider the edge cases too. If the count is zero then the program should return an empty String.

Comments

0
public String repeatSeparator(String word, String sep, int count) {
  String ans ="";
  for(int i=0;i<count;i++)
  {
    if(i<count-1)
    {
      ans+=word + sep;
    } else {
      ans+=word;
    }
  }
  return ans;
}

1 Comment

Welcome to Stack Overflow! Please try to provide an explanation for the code you posted here.
0
public String repeatSeparator(String word, String sep, int count) {
    String s = "";

   if (count > 1)
   {
       while (count > 0)
       {
           if (count > 1)
           {
               s = s + word + sep;
               count--;
           }


           if (count == 1)
           {
               s = s + word;
               count--;
           }
        }
   }

   if (count == 1) s = word;

   return s;

}

Comments

0
public static String repeatSeparator1(String word,String sep,int count){
    StringBuilder sb = new StringBuilder();
    for(int i=0;i<count;i++){
        sb.append(word+sep);
    }
    return sb.substring(0, sb.lastIndexOf(sep));
}

Comments

0

used this:

public String repeatSeparator(String word, String sep, int count) {

  String result = "";

  if(count == 0)
  return "";


  for(int i = 0 ; i < count-1 ; i++){
    result = result +(word+sep);
  }

  return result+word;
}

Comments

0
public String createSeparatorString(String word, String separator, int count) {    
    StringBuffer stringBuffer = new StringBuffer();

    while (count > 0) {
        stringBuffer.append(word);
        count--;
        if (count > 0) {
            stringBuffer.append(separator);
        }
    }

    return stringBuffer.toString();                           
}

Comments

0

Here's another similar approach(I append the word at the end of every string if the count is greater than 0):

public String repeatSeparator(String word, String sep, int count) {
  String out="";
  if(count>0){
    for(int i=1; i<=count-1; i++){
      out += word+sep;
    }
    return out+word;
  }
  else return out;
}

Comments

0
public String repeatSeparator(String word, String sep, int count) {
   String g="";
   for(int i=0;i<count;i++){
     g=g+word;
     if(i<count-1){
       g=g+sep;
     }
  }
   return g;
}

Comments

0
public static void main(String[] args) {

        String str1="Word";
        String str2="X";
        int x=2;

        System.out.println(repeatString(str1, str2, x));

    }

    public static String repeatString(String s1, String s2, int y) {
        String dummy="";
        for(int i=1; i<=y; i++) {
            dummy+=s1;
                if(i!=y) {
                    dummy+=s2;  
                }

        }

        return dummy;

    }

1 Comment

While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.

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.