gudii9
asked on
string concatenation challenge
Hi,
I am working on below challenge
http://codingbat.com/prob/p117334
I wrote 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
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 belowExpected 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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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();
}
ASKER
some reason stringbuilder is not coming to mind.
Let me read these solutions more detail once i get more time
Let me read these solutions more detail once i get more time
StringBuilder is useful so yes do read the lit on it.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window