Problem:
Given a string, return a string length 2 made of its first 2 chars. If the string length is less than 2, use '@' for the missing chars.
atFirst("hello") → "he"
atFirst("hi") → "hi"
atFirst("h") → "h@"
Solution:
public String atFirst(String str) { if (str.length() >= 2) return str.substring(0,2); else if (str.length() == 1) return str + "@"; else return "@@"; }
public String atFirst(String str) {
ReplyDeleteif(str.equals(""))
return str + "@" +"@";
if(str.length()<2)
return str +"@";
String s = str.substring(0,2);
return s;
}
public String atFirst(String str) {
ReplyDeleteif(str.length()==0)
{
return "@@";
}
if(str.length()<2)
{
return str+"@";
}
return str.substring(0,2);
}
public String atFirst(String str) {
ReplyDeletereturn str.length()>=2?str.substring(0,2):str.length()==1?str+"@":str+"@@";
}
public String atFirst(String str) {
ReplyDeleteif(str.isEmpty()){
return "@@";
}
if(str.length() == 1){
return str + '@';
}
return "" + str.charAt(0) + str.charAt(1);
}
“TUIT” -> “UI”
ReplyDeletesorry
public String atFirst(String str) {
ReplyDeleteString a="@";
if(str.length()>=2){
return str.substring(0,2);
}
if(str.length()<2 && str.length()>0){
return str.substring(0).concat("@");
}
if(str.isEmpty()){
return a+a;
}return str;
}
public String lastChars(String a, String b) {
ReplyDeleteString first = "";
first = a.length()< 1 ? "@" : a.substring(0,1);
String last = "";
last = b.length()< 2 ? "@" : b.substring(b.length() -1, b.length());
return first + last;
}