FunctionRacerProblemsRace

Center in a Field

BeginnerStringsPython, JavaScript, TypeScript, Java, C++

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

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Strings problems