Java > String-2 > repeatSeparator (CodingBat Solution)

Problem:

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"


Solution:

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


29 comments :

  1. {
    String mix = "";

    if(word.length() == 0 || count == 0) return "";

    for(int i=0; i < count; i++){
    mix = mix + word + sep;
    }

    if(count >= 1){
    mix = mix.substring(0 , mix.length() - sep.length() );
    }

    return mix;
    }

    ReplyDelete
  2. String result = word;

    if(count==0)
    return "";

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

    }


    return result;

    ReplyDelete
    Replies

    1. public String repeatSeparator(String word, String sep, int count) {
      String temp="";
      for(int a=0;a<count;a+=1)
      {
      temp=temp+word+sep;
      }
      return temp.substring(0,temp.length()-sep.length());
      }

      Delete
  3. String s="";
    if(count>0){
    for(int i=0;i<count-1;i++)
    {
    s = s + word + sep;
    }
    return s+word;}
    return s;

    ReplyDelete
  4. public String repeatSeparator1(String word, String sep, int count) {

    String str1="";

    for(int i = count; i > 0; i--){
    str1 += word + sep;
    }
    return str1.substring(0,str1.length()-sep.length());

    }

    ReplyDelete
  5. public String repeatSeparator(String word, String sep, int count) {
    String res = "";

    if( count == 0) return res;

    res = word;

    for(int i = 1; i < count; i++){
    res += sep + word;
    }

    return res;
    }

    ReplyDelete
  6. public String repeatSeparator(String word, String sep, int count) {
    String res = "";

    if( count == 0) return res;

    res = word;

    for(int i = 1; i < count; i++){
    res += sep + word;
    }

    return res;
    }

    ReplyDelete
  7. public String repeatSeparator(String word, String sep, int count) {
    String result="";
    if(count==1){
    return word;
    }
    if(count==0){
    return result;
    }
    for(int i=0;i<count;i++){

    if(i==count-1){
    result+=word;
    }
    else{

    result+=word+sep;
    }
    }

    return result;
    }

    ReplyDelete
  8. This is my solution.

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

    c+=word+sep;
    }
    return c+word;
    }

    ReplyDelete
  9. Look.
    If count is for example 3 word will be 3 times and sep count-1.So i did this :

    public String repeatSeparator(String word, String sep, int count) {
    String c="";
    for(int i=0;i<count;i++){

    c+=word+sep;
    }

    return c.substring(0,c.length()-sep.length());
    }

    ReplyDelete
  10. String result = word;
    if (count==0)
    return "";
    for (int i=1 ; i< count ; i++)
    result =result+sep+word;
    return result;

    ReplyDelete
  11. String renwe = "";
    for(int i =0;i<count;i++) {
    renwe += word + sep;
    if(i ==count-1) {
    renwe = renwe.substring(0,renwe.length()-sep.length());
    }
    }
    return renwe;

    }

    ReplyDelete
  12. public String repeatSeparator(String word, String sep, int count) {
    String result = "";
    for ( int i = 0; i < count ; i++){
    result = result + word + sep;
    }
    result = result.substring(0,result.length() - sep.length());
    return result;
    }

    ReplyDelete

  13. String temp="";
    int a=0;

    OUTER: while(a!=count){
    temp+=word;
    a++;
    INNER: while(a!=count){
    temp+=sep;
    continue OUTER;
    }
    }
    return temp;

    ReplyDelete
  14. Imagine using loops when your solution could be this simple:


    public String repeatSeparator(String word, String sep, int count) {
    if(count==0) return "";
    if(count==2) return (word+sep+word);
    if(count==3) return (word+sep+word+sep+word);
    if(count==4) return (word+sep+word+sep+word+sep+word);
    if(count==5) return (word+sep+word+sep+word+sep+word+sep+word);
    if(count==7) return (word+sep+word+sep+word+sep+word+sep+word+sep+word+sep+word);
    return word;
    }

    ReplyDelete
  15. int a=count;String str1="";
    for(int j=0;j1)
    {
    str1+=sep;
    count--;
    }
    }
    return str1;

    ReplyDelete
  16. public String repeatSeparator(String word, String sep, int count) {
    String newWord = "";

    if(count == 1){
    return word;
    }

    for(int i = 0; i < count; i++){
    newWord = newWord + word + sep;
    }
    int s = newWord.lastIndexOf(sep);
    return newWord.substring(0,s);
    }

    ReplyDelete
  17. public String repeatSeparator(String word, String sep, int count) {
    String newWord="";
    for(int i=0;i<count;i++){

    if(i==0){
    newWord=word;
    }
    else{
    newWord +=sep+word;
    }
    }

    return newWord;

    }

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

    ReplyDelete
  19. public String repeatSeparator(String word, String sep, int count) {
    String rep = "";
    for (int i = 0; i < count; i++)
    {
    rep += (word + sep);
    }
    return rep.substring(0, rep.length() - sep.length());
    }

    ReplyDelete
  20. public String repeatSeparator(String word, String sep, int count) {
    if (count == 0 || word.isEmpty()){
    return "";
    }
    if(count == 1){
    return word;
    }
    return word + sep + repeatSeparator(word, sep, count - 1);
    }

    ReplyDelete
  21. public String repeatSeparator(String word, String sep, int count) {
    if (count == 0 || word.isEmpty()){
    return "";
    }
    return (count == 1)?
    word:
    word + sep + repeatSeparator(word, sep, count - 1);
    }

    ReplyDelete
  22. public String repeatSeparator(String word, String sep, int count) {
    return (count == 0 || word.isEmpty())? "" : (count == 1)?
    word : word + sep + repeatSeparator(word, sep, count - 1);
    }

    ReplyDelete
  23. String newStr="";
    for(int i=0;i<count;i++){
    newStr +=word+sep;
    }
    int len = (word.length()*count)+(sep.length()*(count-1));
    String s=newStr.substring(0,len);
    return s;

    ReplyDelete
  24. public String repeatSeparator(String word, String sep, int count)
    {
    String s = "";

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

    return s;
    }

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

    ReplyDelete
  26. String s=((count>0)?(word):(""));
    for(int i=0; i<count-1; i++)s+=sep+word;
    return s;

    ReplyDelete
  27. Another way

    public String repeatSeparator(String word, String sep, int count) {
    return String.join(sep, Collections.nCopies(count, word));
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact