X655: Words less than 20

Write a function fullWords(s) that returns a string no longer than 20 characters. If the string s is longer than 20 characters, just truncate at the space closest to the 20th position (so no word is truncated) and return it. If the string s is shorter 20 characters, just return it unchanged.

Examples:

fullWords("Injustice anywhere is a threat to justice everywhere.") -> "Injustice anywhere"
fullWords("Is a threat to justice everywhere.") -> "Is a threat to"
fullWords("Justice everywhere.") -> "Justice everywhere."

Your Answer:

Reset

Practice a different Java exercise

Feedback

Your feedback will appear here when you check your answer.