CodingBat Java String-1 Solutions (All Problems)
CodingBat's Java String-1 set is the introductory string-manipulation track that thousands of CS-1 students work through every semester. Here's a walk-through of the patterns the problems share, with idiomatic Java solutions for the trickier ones.
CodingBat is the practice site Nick Parlante built when he was teaching CS at Stanford, and the Java String-1 set is the introductory string-manipulation track that gets assigned in roughly half the intro CS classes that use Java. The problems are short, the test cases are good, and the platform's instant feedback is the right kind of friction for a beginner. The patterns the problems share are also worth pulling out, because they show up everywhere in real code once you know to look for them.
The Patterns That Repeat
Two methods do most of the work in String-1: String.length() and String.substring(). Knowing the substring overloads cold (substring(start) and substring(start, end)) is the difference between the problems feeling tedious and feeling fluid. The end index is exclusive, which is the most common bug in the early problems.
Concatenation with the + operator builds new strings. Java strings are immutable, so every concatenation creates a new String object. For these short problems that's fine; in production code it's a performance footgun if you're concatenating in a loop. StringBuilder is the right tool for that, but the String-1 set deliberately doesn't use it.
The Easy Problems
helloName: Return "Hello " + name + "!". One line.
makeAbba: Return a + b + b + a. One line.
makeTags: Return "<" + tag + ">" + word + "</" + tag + ">". One line.
makeOutWord: out is the wrapper, word is the middle. Return out.substring(0, 2) + word + out.substring(2). The slice is the only trick; the wrapper is always exactly four characters.
extraEnd: Return the last two characters three times. Use str.substring(str.length() - 2) to get the last two, then concatenate three copies.
The Slightly Trickier Ones
firstTwo: Return the first two characters, or the whole string if it's shorter than two characters. Guard against the short case: if (str.length() < 2) return str; then return str.substring(0, 2);.
firstHalf: Assume even length. Return the first half. return str.substring(0, str.length() / 2);.
withoutEnd: Return the string without the first and last characters. Assume length at least 2. return str.substring(1, str.length() - 1);.
comboString: Return shorter + longer + shorter. The trick is figuring out which is which: compare lengths with if (a.length() < b.length()) and assign accordingly.
nonStart: Return both strings without their first characters, concatenated. return a.substring(1) + b.substring(1);.
The Ones Beginners Get Stuck On
left2: Return the string rotated two characters to the left. The first two move to the end. Idiomatic: return str.substring(2) + str.substring(0, 2);.
right2: Same idea, rotated right by two. return str.substring(str.length() - 2) + str.substring(0, str.length() - 2);.
theEnd: Return the first or last character based on a boolean. return front ? str.substring(0, 1) : str.substring(str.length() - 1);.
startWord: Return the first word of str if it starts with the same character as word, otherwise empty. The trick is that "same character" means specifically the first character, and the comparison is case-sensitive. Substring out the first character of each, compare, and if they match, return str.substring(0, word.length()).
The Pattern to Internalize
Almost every String-1 problem reduces to: figure out the indices, slice with substring, concatenate the pieces. The off-by-one errors come from forgetting that substring(start, end) is exclusive on end. The length-related errors come from forgetting to check whether the string is long enough to do what the problem asks.
The bigger lesson: in Java (and most languages), string manipulation is mostly indexing into known positions and slicing. The advanced techniques (regex, StringBuilder, char arrays) come later. Mastering substring and length first makes everything else easier. CodingBat's String-1 is mostly drill on those two methods, and the drill is the point.
Written by
Tech Talk News Editorial
Tech Talk News covers engineering, AI, and tech investing for people who build and invest in technology.