Task1
We justify the text according to the given width:
#!/usr/bin/env perl
use strict;
use warnings;
sub text_justifier{
my $diff = $_[1] - length($_[0]);
die "Length too short!" if($diff < 0);
my $l = int($diff / 2);
my $r = $diff - $l;
'*' x $l . $_[0] . '*' x $r
}
printf "%s\n",text_justifier('Hi',5);
printf "%s\n",text_justifier('Code',10);
printf "%s\n",text_justifier('Hello',9);
printf "%s\n",text_justifier('Perl',4);
printf "%s\n",text_justifier('A',7);
printf "%s\n",text_justifier('',5);
Task2
We sort the case-folded words and join them back into a string:
#!/usr/bin/env perl
use strict;
use warnings;
use feature qw(fc);
sub word_sorter{
join ' ',sort {fc($a) cmp fc($b)} split /\s+/,$_[0];
}
printf "%s\n",word_sorter('The quick brown fox');
printf "%s\n",word_sorter('Hello World! How are you?');
printf "%s\n",word_sorter('Hello');
printf "%s\n",word_sorter('Hello, World! How are you?');
printf "%s\n",word_sorter('I have 2 apples and 3 bananas!');