String Manipulation
string-manipulation
Practice Problems
Encode and Decode Strings
Design an algorithm to encode a list of strings into a single string and decode it back, handling any character content.
Reverse Words in a String
Reverse the order of words in a string, handling leading/trailing spaces and multiple spaces between words.
Code Snippets
Format Strings with String.format
`String.format` is Java's printf-style formatter for building human-readable strings without ad-hoc concatenation. This snippet covers the common conversions (`%s`, `%d`, `%.2f`, `%n`), padding and alignment for table layouts, and locale-aware vs locale-independent formatting. Reach for `String.format` for any output that mixes types or needs precise width control.
Split a String by Delimiter
C++ does not ship with a one-call `split` function, so this snippet shows three idiomatic alternatives: a `std::stringstream` plus `std::getline` walk for single-character delimiters, a `find`/`substr` loop for multi-character delimiters, and a regex-based split for full pattern flexibility. Pick stringstream for whitespace, find/substr for fixed strings, and regex only when the rules are genuinely complex.
Longest String and String-Length Maps
Two small but common questions on arrays of strings: which string is the longest, and how long is each one. The longest-string answer is a single `reduce`, with the tie-breaking rule explicit. The length-map answer is a single `map`, plus a tiny extension that sorts by length so the longest comes first. Both are short, but the patterns generalize to any "pick an extremum" or "shape the data for display" task.
String Tricks: Anagrams, Vowels, Masking, Extension Check, Find Duplicates, Extract Numbers
A grab-bag of small string utilities pulled from a much larger pool: inspecting strings (anagram check, find duplicate characters, distinguish literal from object), counting and scanning (vowels via regex, extract numbers, extension check), transforming (mask the middle, generate alphabet ranges), and order-aware tricks (remove adjacent duplicates, reverse only words longer than n). Each is short on its own; together they cover most of the string work that shows up in real code.
Question Banks
JavaScript String Manipulation Challenges
Six small-but-tricky string problems: longest-word slicing, list extraction, manual reversal, palindrome checks, anagram detection, and capitalizing every word.
JavaScript Vowel Counting: Three Approaches Quiz
Count vowels in a string three different ways (loop + Set lookup, split + forEach, regex match) plus two companion drills on case sensitivity and big-O analysis.
JavaScript X and O Balance: Two Approaches Quiz
Check whether a string contains an equal count of `x` and `o` (case-insensitive), two ways (regex match-array length and filter + length), plus companions on counting any pair of characters and treating empty input.
