The following question was posted by @ruhroe about an hour ago. I was about to post an answer when it was taken down. That's unfortunate, as I thought it was rather interesting. I'm putting it back up in case the OP sees this and also to give others an opportunity to post solutions.
The original question (which I've edited):
The problem is to split a string on some spaces in the string, based on criteria which depend in part on a number given by the user. If that number were, say, 5, each substring would contain either:
- one word having 5 or more characters or
- as many consecutive words (separated by spaces) as possible, provided the resulting string has at most 5 characters.
For example, if the string were:
"abcdefg fg hijkl mno pqrs tuv wx yz"
the result would be:
["abcdefg", "fg", "hijkl", "mno", "pqrs", "tuv", "wx yz"]
"abcdefg"
is on a separate line because it has at least five characters."fg"
is on a separate line because"fg"
contains 5 or few characters and when combined with the following word, with a space between them, the resulting string,"fg hijkl"
, contains more than 5 characters."hijkl"
is on a separate line because it satisfies both criteria.
How can I do that?