Solved

# string concatenation challenge

Posted on 2014-10-18
226 Views
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;
}
``````
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("There") → "TThTheTherThere"      "There"      X
stringSplosion("Kitten") → "KKiKitKittKitteKitten"      "Kitten"      X
stringSplosion("Bye") → "BByBye"      "Bye"      X
stringSplosion("Good") → "GGoGooGood"      "Good"      X

0
Question by:gudii9
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 20

Assisted Solution

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;
}
``````
0

LVL 16

Accepted Solution

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();

}
``````
0

LVL 20

Expert Comment

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;
}
``````
0

LVL 16

Expert Comment

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();
}
``````
0

LVL 7

Author Comment

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

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

LVL 32

Assisted Solution

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

web application structure 18 129
Java syntax, or is it Selenium 6 59
Need Help! Getting a syntax error and don't understand why 3 54