CodingBat: Java. String-1, Part I


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


In the String-1 section on CodingBat you have the chance to familiarize yourself with basic string operations. The exercises do get a bit repetitive, but you should be able to quickly go through all of them and move on to more challenging parts.

All solutions were successfully tested on 19 January 2013.

helloName:

public String helloName(String name) {
		return "Hello " + name + "!";
}

makeAbba:

public String makeAbba(String a, String b) {
		return a + b + b + a;
}

makeTags:

public String makeTags(String tag, String word) {
		return "<" + tag + ">" + word + "</" + tag + ">";
}

makeOutWord:

public String makeOutWord(String out, String word) {
		return out.substring(0,2) + word + out.substring(2);
}

extraEnd:

public String extraEnd(String str) {
		int len = str.length();
		return str.substring(len - 2) + str.substring(len - 2)
				+ str.substring(len - 2);
}

firstTwo:

public String firstTwo(String str) {
		if (str.length() < 2) return str;
		return str.substring(0, 2);
}

firstHalf:

public String firstHalf(String str) {
		return str.substring(0, str.length()/2);
}

withoutEnd:

public String withoutEnd(String str) {
		return str.substring(1, str.length() - 1);
}

comboString:

public String comboString(String a, String b) {  
		if (a.length() > b.length()) return b + a + b;
		return a + b + a;
}

nonStart:

public String nonStart(String a, String b) {
		return a.substring(1) + b.substring(1);
}

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


9 thoughts on “CodingBat: Java. String-1, Part I

  1. Pingback: Work | namfam

  2. a cheater dude

    m uch knowledge
    r espectable code
    s uper good learning skills

    R eally good pieces of code

    i understand everything now
    s uperbly done

    g reat
    a mazing
    y do i understand everything?

    Reply

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.