#C7632. String Reformatter
String Reformatter
String Reformatter
You are given a string s
and a positive integer n
. Your task is to reformat the string by removing all spaces and then inserting a space after every n characters.
In other words, let \( s' \) be the string obtained from s
by removing all spaces. Then, partition \( s' \) into segments, where each segment (except possibly the last one) contains exactly \( n \) characters. Finally, join these segments with a single space between them. Formally, if \( s' = c_1c_2\ldots c_m \), you need to output:
\[
\text{output} = c_1c_2\ldots c_n\;\; c_{n+1}c_{n+2}\ldots c_{2n}\;\; \ldots \;\; c_{kn+1}\ldots c_{m}
\]
If the string is empty (or contains only spaces), output an empty string.
inputFormat
The input consists of two lines. The first line contains the string s
(which may include spaces). The second line contains a single positive integer n
, which represents the segment length.
outputFormat
Output a single line containing the reformatted string as described above.
## sampleThis is a test
4
This isat est