LeftPad in Perl
We'll cover the following...
Press + to interact
use feature qw(say);sub leftPad {local($str, $paddedLength, $ch) = @_;if ($paddedLength <= length($str)) {return $str;}$ch = "." unless (defined($ch));if (length($ch) != 1) {say "Invalid input";return $str;}$paddedLength = $paddedLength - length($str);return ($ch x $paddedLength) . $str;}say leftPad("1", 1);say leftPad("2", 2);say leftPad("3", 3);say leftPad("4", 4);say leftPad("5", 5);say leftPad("hello", 7);say leftPad("foo", 6);say leftPad("foo", 3);say leftPad("foobar", 3);say leftPad("foo", 6, "?");
to save progress
LeftPad in Go
LeftPad in C#
to save progress