Detecting word boundaries to extract initials.
Spaces (and the start of the string) mark where words begin. The first letter of each word forms the initials.
A word boundary is either the start of the String or a position right after a space. The character at a word boundary is the first letter of that word — exactly what initials are made of.
The classic FRQ: given "Maria Carlos Rodriguez", return "MCR". The algorithm scans the String and grabs the character whenever a boundary is detected.
On the AP exam, word-boundary problems appear in name-parsing and acronym FRQs.
First char is always taken; later chars only after a space.
Two rules combined into one loop.
Initials extraction
public String getInitials(String name) {
if (name.length() == 0) return "";
String initials = "" + name.charAt(0); // first letter always
for (int i = 1; i < name.length(); i++) {
if (name.charAt(i - 1) == ' ' && name.charAt(i) != ' ') {
initials += name.charAt(i);
}
}
return initials;
}
Why check the previous char
The boundary test asks "is this position right after a space?" That's charAt(i - 1) == ' '. Combined with "and not itself a space", we identify the start of each word.
Edge cases
- Empty string: return empty initials.
- Single word: return just the first letter.
- Multiple spaces in a row: the extra
!= ' 'check prevents grabbing nothing. - Leading space: handled by the loop starting at index 1.
Convert to uppercase
initials = initials.toUpperCase();
Most prompts want uppercase initials. Apply at the end or per-character.
Word-boundary vocabulary.
Used in name parsing.
- Word boundary — position where a new word begins.
- Initials — first letters of each word.
- Whitespace — space, tab, newline characters.
- Previous-char check — looking back to detect boundaries.
- Edge case — input that needs special handling (empty, single word, etc.).
How word boundaries are tested.
Previous-char detection and boundary edge cases dominate.
- First char is always a word start. Handle outside the loop.
- Check
charAt(i - 1) == ' 'for "after space". - Skip leading/double spaces. Combined check prevents empty matches.
- Start loop at index 1. Avoids out-of-bounds on
i - 1.
Extract initials from "Maria Carlos Rodriguez".
Trace the boundary detection.
Input: "Maria Carlos Rodriguez".
- First char 'M' → initials = "M".
- i=5: charAt(4)=' ', charAt(5)='C' → boundary. initials = "MC".
- i=12: charAt(11)=' ', charAt(12)='R' → boundary. initials = "MCR".
- All other positions: previous char is a letter; no append.
Final: "MCR".
For " Bob" (leading double space):
- First char ' ' → initials = " " (problem: starts with space).
- To fix: skip leading whitespace before grabbing the first letter, or check the first char isn't a space.
Word-boundary pitfalls.
Most miss the first letter or grab spaces.
- Forgetting the first char. Loop only catches boundaries after spaces.
- Loop from index 0 checking
i - 1. Crashes — start at 1. - Grabbing the space. Without the
!= ' 'check, double spaces produce empty initials. - Empty input. charAt(0) crashes on empty string.
Boundary detection logic.
Two conditions, combined.
Word Boundary Reference
AP Quick Reference| Position | Condition | Action |
|---|---|---|
| Index 0 | Always a boundary | Grab the char. |
| Interior | charAt(i-1) == ' ' && charAt(i) != ' ' | Grab the char. |
| Loop start | i = 1 | Avoids OOB. |
| Edge: empty | length == 0 | Return "". |
How to master word boundaries.
First char separately; then scan for "after space".
- MCQ Practice — for each option, verify the first char is handled and loop starts at 1.
- Java Lab — write getInitials, countWords, capitalizeWords. Test on names with double spaces.
- FRQ Practice — handle the first char outside the loop; then check
i - 1inside.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 12 Topic 08 Word Boundaries and Initials FRQ Practice
AP-style topic practice assessment