diff options
| author | wanderdoc <wanderdoc@users.noreply.github.com> | 2025-07-25 19:52:43 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-25 19:52:43 +0200 |
| commit | 91630bebdb0dcfa3d03f37c8ced75c619d9a01b5 (patch) | |
| tree | fac67d481d039059342bdeb608cdd2e9fd0bf580 | |
| parent | 16b3084d799d0ae45315f2db5bb637d09bc4b9b5 (diff) | |
| download | perlweeklychallenge-club-91630bebdb0dcfa3d03f37c8ced75c619d9a01b5.tar.gz perlweeklychallenge-club-91630bebdb0dcfa3d03f37c8ced75c619d9a01b5.tar.bz2 perlweeklychallenge-club-91630bebdb0dcfa3d03f37c8ced75c619d9a01b5.zip | |
Create ch-1.pl
| -rw-r--r-- | challenge-331/wanderdoc/perl/ch-1.pl | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/challenge-331/wanderdoc/perl/ch-1.pl b/challenge-331/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..7d213b6679 --- /dev/null +++ b/challenge-331/wanderdoc/perl/ch-1.pl @@ -0,0 +1,39 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given a string. +Write a script to find the length of last word in the given string. + +Example 1 + +Input: $str = "The Weekly Challenge" +Output: 9 + + +Example 2 + +Input: $str = " Hello World " +Output: 5 + + +Example 3 + +Input: $str = "Let's begin the fun" +Output: 3 +=cut + +use Test2::V0 -no_srand => 1; +is(last_word_length("The Weekly Challenge"), 9, "Example 1"); +is(last_word_length(" Hello World "), 5, "Example 2"); +is(last_word_length("Let's begin the fun"), 3, "Example 3"); +done_testing(); + + +sub last_word_length +{ + my $str = $_[0]; + return length((grep {length($_) > 0} + split(/\s/,$str))[-1]); +} |
