Intern — Middle
Truncating a string to a specified number of words
livecode
Task condition
Given a sentence sentence, which is a string of words separated by spaces, and an integer limit. The task is to shorten the sentence so that it contains only the first limit words. Return the resulting string.
Example 1: Input: sentence = "Hello how are you today", limit = 4 Output: "Hello how are you" Explanation:
- Original sentence: "Hello how are you today" (5 words).
- Take the first 4 words: "Hello how are you".
- Result: "Hello how are you".
Example 2: Input: sentence = "What is the weather like", limit = 3 Output: "What is the" Explanation:
- Original sentence: "What is the weather like" (5 words).
- Take the first 3 words: "What is the".
- Result: "What is the".
Example 3: Input: sentence = "chopper is not a tanuki", limit = 5 Output: "chopper is not a tanuki" Explanation:
- Original sentence: "chopper is not a tanuki" (5 words).
limitequals the number of words, so return the full sentence.