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"
doubleChar("The") → "TThhee"
doubleChar("AAbb") → "AAAAbbbb"
doubleChar("Hi-There") → "HHii--TThheerree"
Solution:
public String doubleChar(String str) {
int len = str.length();
String new_str = "";
for (int i = 0; i < len; i++) {
new_str += str.substring(i,i+1) + str.substring(i, i+1);
}
return new_str;
}
public String doubleChar(String str) {
ReplyDeleteString newString = "";
for(int i=0; i<str.length(); i++){
newString = newString + str.charAt(i)+str.charAt(i);
}
return newString;
}
public String doubleChar(String str) {
ReplyDeletereturn (str.isEmpty())? "": "" + str.charAt(0) + str.charAt(0) +doubleChar(str.substring(1));
}
public String doubleChar(String str) {
ReplyDeleteif(str.isEmpty()){
return ""
}
return str.charAt(0) + str.charAt(0) +doubleChar(str.substring(1));
}
public String doubleChar(String str) {
ReplyDeleteif(str.isEmpty()){
return "";
}
return "" + str.charAt(0) + str.charAt(0) +doubleChar(str.substring(1));
}
public String doubleChar(String str) {
ReplyDeleteif(str.isEmpty()){
return "";
}
return "" + str.charAt(0) + str.charAt(0) +doubleChar(str.substring(1));
}
public String doubleChar(String str) {
ReplyDeletereturn (str.isEmpty())?
str:
"" + str.charAt(0) + str.charAt(0) +doubleChar(str.substring(1));
}