Padding and sorting
Weekly challenge 360 — 9 February 2026
Week 360: 9 Feb 2026
You are given a string and a width. Write a script to return the string that centers the text within that width using asterisks * as padding.
Example 1 Input: $str = 'Hi', $width = 5 Output: '*Hi**' Text length = 2, Width = 5 Need 3 padding characters total Left padding: 1 star, Right padding: 2 stars Example 2 Input: $str = 'Code', $width = 10 Output: '***Code***' Text length = 4, Width = 10 Need 6 padding characters total Left padding: 3 stars, Right padding: 3 stars Example 3 Input: $str = 'Hello', $width = 9 Output: '**Hello**' Text length = 5, Width = 9 Need 4 padding characters total Left padding: 2 stars, Right padding: 2 stars Example 4 Input: $str = 'Perl', $width = 4 Output: 'Perl' No padding needed Example 5 Input: $str = 'A', $width = 7 Output: '***A***' Text length = 1, Width = 7 Need 6 padding characters total Left padding: 3 stars, Right padding: 3 stars Example 6 Input: $str = '', $width = 5 Output: '*****' Text length = 0, Width = 5 Entire output is padding
This is not a hard challenge, and as in many previous ones it can probably be done with a single rather impenetrable regular expression. I'll leave that for others to do.
I took the simple approach of calculating the number of asterisks required:
$asterisks = $width - length($string);
and then concatenating
int($asterisks / 2) asterisks
($asterisks - int($asterisks / 2)) asterisks.That copes with all the edge cases such as an empty
string, a zero $width with an empty string, and a string
with just one fewer characters than $width.
And lastly I retun an error meesage if $string is
longer than $width.
#!/usr/bin/perl # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge use v5.26; # The Weekly Challenge - 2026-02-09 use utf8; # Week 360 - task 1 - Text justifier use warnings; # Peter Campbell Smith binmode STDOUT, ':utf8'; use Encode; text_justifier('Code', 10); text_justifier('Toad', 11); text_justifier('Road', 4); text_justifier('Mode', 20); text_justifier('Node', 21); text_justifier('Bode', 5); text_justifier('', 1); text_justifier('', 0); sub text_justifier { my ($string, $width, $length, $asterisks, $left, $output); # initialise ($string, $width) = @_; say qq[\nInput: \$string = '$string', \$width = $width]; # how many asterisks? $asterisks = $width - length($string); # we can do it if ($asterisks >= 0) { $left = int($asterisks / 2); say qq[Output: ] . ('*' x $left) . $string . ('*' x ($asterisks - $left)); # but not if the string won't fit } else { say qq[Output: Error: string is longer than width]; } }
Input: $string = 'Code', $width = 10 Output: ***Code*** Input: $string = 'Toad', $width = 11 Output: ***Toad**** Input: $string = 'Road', $width = 4 Output: Road Input: $string = 'Mode', $width = 20 Output: ********Mode******** Input: $string = 'Node', $width = 21 Output: ********Node********* Input: $string = 'Bode', $width = 5 Output: Bode* Input: $string = '', $width = 1 Output: * Input: $string = '', $width = 0 Output:
Any content of this website which has been created by Peter Campbell Smith is in the public domain