30: repeatEnd Write a function in Java that implements the following logic: Give
ID: 3846449 • Letter: 3
Question
30: repeatEnd
Write a function in Java that implements the following logic: Given a string and an int n, return a string made of n repetitions of the last n characters of the string. You may assume that n is between 0 and the length of the string, inclusive.
public String repeatEnd(String str, int n)
{
}
33: zipZap
Given a string str, find all places where a three-letter combination starting with "z" and ending with "p" occurs. Return a string where for all such three-letter sequences, the middle letter has been removed. For example, a string like "zipXzap" would produce a result of "zpXzp".
public String zipZap(String str)
{
}
35. countCode
Write a function in Java that counts the number of times the string "code" appears anywhere in the given string str, except that we'll accept any letter in place of the 'd', so for example, "cope" and "cooe" count.
public int countCode(String str)
{
}
37. endOther
Given two strings, return true if either of the strings appears at the very end of the other string, ignoring upper/lower case differences (in other words, the computation should not be "case sensitive"). Note: str.toLowerCase() returns the lowercase version of a string.
public boolean endOther(String a, String b)
{
}
38: xyBalance
We'll say that a string is xy-balanced if for all the 'x' characterss in the string, there exists a 'y' character somewhere later in the string. So "xxy" is balanced, but "xyx" is not. One 'y' can balance multiple 'x's. Return true if the given string is xy-balanced.
public boolean xyBalance(String str)
{
}
Explanation / Answer
33: zipZap
String zipZap(String s) {
String output = "";
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'z' && s.charAt(i + 2) == 'p') {
output += s.charAt(i) + "" + s.charAt(i + 2);
i = i + 2;
} else {
output += s.charAt(i);
}
}
return output;
}
33: zipZap
String zipZap(String s) {
String output = "";
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'z' && s.charAt(i + 2) == 'p') {
output += s.charAt(i) + "" + s.charAt(i + 2);
i = i + 2;
} else {
output += s.charAt(i);
}
}
return output;
}
35: countCode
int countCode(String s) {
int n = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'c' && s.charAt(i + 1) == 'o' && s.charAt(i + 3) == 'e') {
n++;
i = i + 4;
}
}
return n;
}
37: endOther
boolean endOther(String s1,String s2)
{
s1=s1.toLowerCase();
s2=s2.toLowerCase();
if((s1.length()>s2.length()) && (s1.substring(s1.length()-s2.length())).equals(s2))
return true;
else if((s1.length()<s2.length()) && (s2.substring(s2.length()-s1.length())).equals(s1))
return true;
else
return false;
}
38: xyBalance
boolean xyBalance(String s)
{
if(s.charAt(s.length()-1)=='y')
return true;
else
return false;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.