Problem:
Given a string and an int n, return a string made of the first and last n chars from the string. The string length will be at least n.
nTwice("Hello", 2) → "Helo"
nTwice("Chocolate", 3) → "Choate"
nTwice("Chocolate", 1) → "Ce"
Solution:
public String nTwice(String str, int n) { return str.substring(0,n) + str.substring(str.length()- n, str.length()); }
public String nTwice(String str, int n) {
ReplyDeletereturn str.substring(0,n).concat(str.substring(str.length()-n));
}
public String nTwice(String str, int n) {
ReplyDeleteString a=str.substring(0,n);
String b=str.substring(str.length()-n);
return a+b;
}
public String nTwice(String str, int n) {
ReplyDeleteString res="";
String fst=str.substring(0,n);
String lst=str.substring(str.length()-n,str.length());
return res=fst+lst;
}
public String nTwice(String str, int n) {
ReplyDeleteif(str=="")
{
return str;
}
else
{
int ub=str.length()-1;
return str.substring(0,n) + str.substring(ub-n+1,str.length());
}
}