Word Wrap

Write a function that takes two arguments, a string, and a column number. The function returns the string, but with line breaks inserted at just the right places to make sure that no line is longer than the column number. You try to break lines at word boundaries.

Like a word processor, break the line by replacing the last space in a line with a newline.


This kata was invented by Robert C. Martin and presented on his blog. It is also described on codingdojo.org and cyber-dojo. This description takes inspiration from all those sources.

Referenced in these Learning Hours: