Build a Sentence with +=
Easyjava
Read several words and join them into one sentence using += on a String.
Input: the first line is an integer N; the second line has N words separated by single spaces.
Output: one line — the words joined by single spaces (no leading or trailing space).
Example 1
Input
3 Java is fun
Output
Java is fun
- 1 <= N <= 50
- each word is 1..20 letters
Hint 1
sentence += w + " "; builds the text with a trailing space each time.
Hint 2
sentence.trim() removes the extra space at the end before printing.
+= also concatenates Strings. Append each word followed by a space, then trim the single trailing space at the end. (A StringBuilder would be better for very long input, but += is the point here.)