Center in a Field
Return s centered in a field of width characters, padded with copies of the single character fill.
The number of padding characters is width - len(s). Half of them, rounded down, go to the left of s and the rest go to the right, so when that number is odd the right side gets one more fill than the left. If len(s) >= width, return s unchanged.
Example 1
s "ab"
width 6
fill "*"
returns "**ab**"
Four * are needed; two go on each side.
Example 2
s "ab"
width 5
fill "*"
returns "*ab**"
Three * are needed; one goes on the left and the remaining two on the right.
Constraints
0 <= len(s) <= 200 <= width <= 30fillis exactly one charactershas lowercase letters and digits only
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Strings problems
- Chunks From the LeftBeginner
- Consecutive CharactersBeginner
- Count Capitalized WordsBeginner
- Count Differing PositionsBeginner
- Count Prefixes of a Given StringBeginner
- Defanging an IP AddressBeginner
- Determine if String Halves Are AlikeBeginner
- Find Words Containing CharacterBeginner