Problem:
Given a string, return a string where for every char in the original, there are two chars.
doubleChar("The") → "TThhee"
doubleChar("AAbb") → "AAAAbbbb"doubleChar("Hi-There") → "HHii--TThheerree"
Solution:
public String doubleChar(String str) { String love = ""; for (int i = 0; i <=str.length()-1;i++) { love += str.charAt(i); love += str.charAt(i); } return love; }
Thanks for providing solutions: they have sped up my learning rate.
ReplyDeleteu dont learn by cheating
Deletepublic String doubleChar(String str) {
ReplyDeleteString newS = "";
for (int i = 0; i < str.length(); i++) {
newS = newS + str.charAt(i) + str.charAt(i);
}
return newS;
}
this is wrong
Deleteu gonna have an adress memory as output that way
Deleteits wrong
ReplyDelete< not <=
ReplyDeletewrong
ReplyDeletethanks
ReplyDeletepublic String doubleChar(String str){
ReplyDeleteString result="";
for(int i=0 ; i<=str.length()-1; i++){
result= result+str.subtring(i,i+1)+str.substring(i, i+1);
}
return result;
}
public String doubleChar(String str) {
ReplyDeleteString d="";
for(int i=0;i<str.length();i++){
d=d+str.charAt(i)+str.charAt(i);
}
return d;
}