Solved

string concatenation challenge

Posted on 2014-10-18
7
214 Views
Last Modified: 2014-10-20
Hi,

I am working on below challenge
http://codingbat.com/prob/p117334

I wrote as below
public String stringSplosion(String str) {
  String s="";
  for(int i=0;i<=str.length()-1;i++){
  return (s+str.substring(i));
  
  }
  return null;
}

Open in new window

My test cases failing as below


Expected      Run            
stringSplosion("Code") → "CCoCodCode"      "Code"      X         
stringSplosion("abc") → "aababc"      "abc"      X         
stringSplosion("ab") → "aab"      "ab"      X         
stringSplosion("x") → "x"      "x"      OK         
stringSplosion("fade") → "ffafadfade"      "fade"      X         
stringSplosion("There") → "TThTheTherThere"      "There"      X         
stringSplosion("Kitten") → "KKiKitKittKitteKitten"      "Kitten"      X         
stringSplosion("Bye") → "BByBye"      "Bye"      X         
stringSplosion("Good") → "GGoGooGood"      "Good"      X         
stringSplosion("Bad") → "BBaBad"      "Bad"      X         

How do i fix and improve my code. Please advise
0
Comment
Question by:gudii9
7 Comments
 
LVL 19

Assisted Solution

by:n2fc
n2fc earned 167 total points
ID: 40389342
public String stringSplosion(String str) {
  String s="";
  for(int i=0;i<str.length();i++){
     s=s+str.substring(0,i+1);
  }
  return s;
}

Open in new window

0
 
LVL 16

Accepted Solution

by:
krakatoa earned 166 total points
ID: 40389364
Or
public String stringSplosion(String str) {


StringBuilder sb = new StringBuilder();

for(int y=0;y<str.length()+1;y++){sb.append(str.substring(0,y));}

return sb.toString();

  }

Open in new window

0
 
LVL 19

Expert Comment

by:n2fc
ID: 40389372
Yet another solution (a tad more arcane):

public String stringSplosion(String str) {
   String s="";
   for(int i=0; i<str.length(); s+=str.substring(0, ++i));
   return s;
}

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 16

Expert Comment

by:krakatoa
ID: 40389386
And one more, slightly better incremental model :

public String stringSplosion(String str) {

StringBuilder sb = new StringBuilder();

for(int y=0;y<str.length();y++){sb.append(str.substring(0,y+1));}

return sb.toString();
  }

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 40390039
some reason stringbuilder is not coming to mind.

Let me read these solutions more detail once i get more time
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40390078
StringBuilder is useful so yes do read the lit on it.
0
 
LVL 31

Assisted Solution

by:awking00
awking00 earned 167 total points
ID: 40392213
gudii9,
You have asked numerous questions involving the manipulation of strings. As I have mentioned in the past a String is an object, not a primitive datatype, and is immutable (meaning it can not be changed). However, methods may be applied to a String (e.g. using charArrays, concatenation, substrings, indexesOf, etc.) to create another String with the desired affect. The StringBuilder class was developed primarily to get around this fact. You seem to have eschewed the use of StringBuilder in favor of other methods, which is fine. It points out that there is almost always more than one way of accomplishing objectives in Java. In certain cases, methods other than StringBuilder might be easier to understand and write but, in many other cases, you will find that StringBuilder offers a better means of manipulating Strings. It behooves you to understand the many tools Java provides such as StringBuilder, which krakatoa suggests, and I second, you read up on.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now