Posts Tagged substring
everyNth
Given a non-empty string and an int N, return the string made starting with char 0, and then every Nth char of the string. So if N is 3, use char 0, 3, 6, … and so on. N is 1 or more.
everyNth(”Miracle”, 2) ? “Mrce”
everyNth(”abcdefg”, 2) ? “aceg”
everyNth(”abcdefg”, 3) ? “adg”
public String everyNth(String str, int n) {
//char c[]=str.toCharArray();
String str1="";
for(int i=0;i<str.length();i++)
{ if(i%n ==0)
str1=str1+str.substring(i,i+1);
}
return str1;
}
endUp
Given a string, return a new string where the last 3 chars are now in upper case. If the string has less than 3 chars, uppercase whatever is there. Note that str.toUpperCase() returns the uppercase version of a string.
endUp(”Hello”) ? “HeLLO”
endUp(”hi there”) ? “hi thERE”
endUp(”hi”) ? “HI”
public String endUp(String str) {
if(str.length() < 3) return(str.toUpperCase());
else return str.substring(0,str.length()-3)+str.substring(str.length()-3).toUpperCase();
}
startOz
Given a string, return a string made of the first 2 chars (if present), however include first char only if it is ‘o’ and include the second only if it is ‘z’, so “ozymandias” yields “oz”.
startOz(”ozymandias”) ? “oz”
startOz(”bzoo”) ? “z”
startOz(”oxx”) ? “o”
public String startOz(String str) {
String result = "";
if (str.length() >= 1 && str.charAt(0)=='o') {
result = result + str.charAt(0);
}
if (str.length() >= 2 && str.charAt(1)=='z') {
result = result + str.charAt(1);
}
return result;
}
mixStart
Return true if the given string begins with “mix”, except the ‘m’ can be anything, so “pix”, “9ix” .. all count.
mixStart(”mix snacks”) ? true
mixStart(”pix snacks”) ? true
mixStart(”piz snacks”) ? false
public boolean mixStart(String str) {
if(str.length()<3) return false;
else return(str.substring(1,3).equals("ix"));
}
delDel
Given a string, if the string “del” appears starting at index 1, return a string where that “del” has been deleted. Otherwise, return the string unchanged.
delDel(”adelbc”) ? “abc”
delDel(”adelHello”) ? “aHello”
delDel(”adedbc”) ? “adedbc”
public String delDel(String str) {
if (str.length()>=4 && str.substring(1, 4).equals("del")) {
return str.substring(0, 1) + str.substring(4);
}
return str;
}
startHi
Given a string, return true if the string starts with “hi” and false otherwise.
startHi(”hi there”) ? true
startHi(”hi”) ? true
startHi(”hello hi”) ? false
public boolean startHi(String str) {
if(str.length()<2) return false;
else return(str.substring(0,2).equals("hi"));
}
front22
Given a string, take the first 2 chars and return the string with the 2 chars added at both the front and back, so “kitten” yields”kikittenki”. If the string length is less than 2, use whatever chars are there.
front22(”kitten”) ? “kikittenki”
front22(”Ha”) ? “HaHaHa”
front22(”abc”) ? “ababcab”
public String front22(String str) {
int n=str.length();
if(n<2) return(str+str+str);
else return( str.substring(0,2)+str+str.substring(0,2));
}
backAround
Given a string, take the last char and return a new string with the last char added at the front and back, so “cat” yields “tcatt”. The original string will be length 1 or more.
backAround(”cat”) ? “tcatt”
backAround(”Hello”) ? “oHelloo”
backAround(”a”) ? “aaa”
public String backAround(String str) {
int n;
n=str.length();
if(n==1) return str+str+str;
else return(str.substring(n-1)+str+str.substring(n-1));
}
front3
Posted by admin in Uncategorized on January 11th, 2010
Given a string, we’ll say that the front is the first 3 chars of the string. If the string length is less than 3, the front is whatever is there. Return a new string which is 3 copies of the front.
front3(”Java”) ? “JavJavJav”
front3(”Chocolate”) ? “ChoChoCho”
front3(”abc”) ? “abcabcabc”
public String front3(String str) {
if(str.length() <3) return str+str+str;
else return str.substring(0,3)+str.substring(0,3)+str.substring(0,3);
}
frontBack
Given a string, return a new string where the first and last chars have been exchanged.
frontBack(”code”) ->”eodc”
frontBack(”a”) -> “a”
frontBack(”ab”) -> “ba”
public String frontBack(String str) {
if(str.length()==1) return str;
else return( str.substring(str.length())+str.substring(1,str.length()-1)+str.substring(1));
}


Recent Comments