Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

30: repeatEnd Write a function in Java that implements the following logic: Give

ID: 3846872 • 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

30.

public String repeatEnd(String str, int n)

{

StringBuilder sb =new StringBuilder(n*n); //creates modifiable empty string sb with capacity n*n

String last =str.substring(str.length()-n); // substring of str is considered as the string last upto n characters

for( int i=0; i< n ; i++)

sb.append (last); //adds the string last to sb

return sb.toString(); //returns sb

}

33.

public String zipZap(String str)

{

int len=str.length();

int last=len-2;

int i=0;

char ch;

StringBuilder sb= new StringBuilder (len); //creates modifiable empty string sb

while(i< len)

{

ch= str.charAt(i); // initially pointer is at 0 that is 1st character of the string

if (ch=='z' && i<last && str.charAt(i+2)=='p') //as 'z' is at position 0 , 'A' at 1 'P' at 2 and 'x' at 3 here i+2 is 0+2 should be 'p'

{

sb.append("zp"); //adds the string zp to string sb

i=i+3; // increments the pointer by 3

}

else {

sb.append(ch); // appends to sb whatever other than of Z occurs .eg:x

i=i+1; //increments by 1

}

} //end while

return sb.toString();

}

35.

public int countCode(String str)

{

int count=0;

int i=0;

int len= str.length()-3; //consider word of length 4 in the string

while(i<len)

{

if( str.charAt(i)=='c' &&  str.charAt(i+1)=='o' &&  str.charAt(i+3)=='e' ) // as i+2 can be any letter from the word code

{

count++; //increment code if satisifies

i=i+4;

}

else

{

i=i+1;

}

} //end while

return count;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote