Simple wrapping at word boundaries
Input
wrap_text('The quick brown fox', 10)
Output
'The quick\nbrown fox'
First line 'The quick' length 9 <= 10, second line contains remaining words.
Full lesson preview
Break a string into lines no longer than a given width, breaking on spaces when possible.
Problem statement
Task
Examples
Input
wrap_text('The quick brown fox', 10)
Output
'The quick\nbrown fox'
First line 'The quick' length 9 <= 10, second line contains remaining words.
Input
wrap_text('Supercalifragilisticexpialidocious', 10)
Output
'Supercalif\nragilistic\nexpialidoc\nious'
A single long word is split into chunks of width 10.
Input format
Output format
Constraints
Samples
Input
wrap_text('Hello world', 5)
Output
'Hello\nworld'
Both words fit exactly on separate lines when width is 5.