CodingBat: Java. String-1, Part II

For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.


public String left2(String str) {
		return str.substring(2) + str.substring(0, 2);


public String right2(String str) {
		int len = str.length();
		return str.substring(len - 2) + str.substring(0, len - 2);


public String theEnd(String str, boolean front) {
		if (front) return str.substring(0, 1);
		return str.substring(str.length() - 1);


public String withouEnd2(String str) {
		if (str.length() <= 2) return "";
		return str.substring(1, str.length() - 1);


public String middleTwo(String str) {
		int len = str.length();
		return str.substring(len / 2 - 1, len / 2 + 1);


public boolean endsLy(String str) {
		int len = str.length();
		if (len < 2) return false;
		return (str.substring(len - 2).equals("ly"));


public String nTwice(String str, int n) {
		return str.substring(0, n) + str.substring(str.length() - n);


public String twoChar(String str, int index) {
		if (index < 0 || index + 2 > str.length())
			return str.substring(0, 2);
		return str.substring(index, index + 2);


public String middleThree(String str) {
		int len = str.length();
		if (len == 3) return str;
		return str.substring(len / 2 - 1, len / 2 + 2);


public boolean hasBad(String str) {
		if (str.length() <= 2) return false;
		if (str.length() == 3) return str.substring(0, 3).equals("bad");
		return str.substring(0, 3).equals("bad")
				|| str.substring(1, 4).equals("bad");

For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.

6 thoughts on “CodingBat: Java. String-1, Part II

  1. Brett Campbell

    the answer you have for middletwo does not work but you are not off by much the right is
    int len = str.length();
    return str.substring(len / 2-1 , len / 2 + 2);

  2. Ryan

    Very simple solution for the hasBad puzzle in String-1

    public boolean hasBad(String str) {
    return (str.indexOf(“bad”) == 0 || str.indexOf(“bad”) == 1);

    Might be slightly ahead of the skill level required for this puzzle, but it sure is a tidy solution.

    1. Gregor Ulm Post author

      You are not supposed to use the method indexOf(). Keep in mind that those problems are about teaching students elementary building blocks in order to prepare them for situations in which they cannot simply access a library function.

      1. Frsco

        Your point makes perfect sense, and indexOf() is sometimes seen as a frowned-upon shortcut.
        I noticed that when you finish the problem itself, and you click the button entitled “See Our Solution” , while he does not use indexOf() directly, he states:
        // Alternately one could use indexOf() — that code is short
        This is apparently not the optimized solution, however it provides a very short and succinct way of expressing your ideas. indexOf() soon maybe considered a fundamental and elementary part of Java.

  3. Frsco

    In the middleThree solution, is the if (len == 3) return str; necessary, or is it just a statement that simplifies the code?


Leave a Reply

Your email address will not be published. Required fields are marked *

Spammer prevention; the answer is an integer: * Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.