Posts Tagged string functions

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;
}

, , ,

1 Comment

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();

}

, , , ,

1 Comment

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;
}

, ,

No Comments

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"));
}

, ,

No Comments

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;
}

, ,

No Comments

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"));
}

, , ,

No Comments

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));
}

, , ,

No Comments

front3

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);

}

, ,

No Comments

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));

}

, ,

No Comments

missingChar

Given a non-empty string and an int n, return a new string where the char at index n has been removed. The value of n will be a valid index of a char in the original string (i.e. n will be in the range 0..str.length()-1 inclusive).

missingChar(”kitten”, 1)-> “ktten”
missingChar(”kitten”, 0) -> “itten”
missingChar(”kitten”, 4) ->”kittn”

public String missingChar(String str, int n) {
if(n>=0 && n<=str.length()-1)
{if(str.length()==1) return(" ");
else return( str.substring(0,n)+str.substring(n+1));
}
else return str;
}

, , ,

No Comments