The weekly challenge 254 - Task 2: Reverse Vowels
1 #!/usr/bin/env perl
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 use strict;
44 use warnings;
45
46 reverse_vowels("Raku");
47 reverse_vowels("Perl");
48 reverse_vowels("Julia");
49 reverse_vowels("Uiua");
50
51 sub reverse_vowels {
52 my $s = shift;
53 print "Input: \"$s\"\n";
54 my @chars = split //, $s;
55 my @vowels = ();
56 my @indices = ();
57 foreach my $i (0..$#chars) {
58 if(is_vowel($chars[$i])) {
59 push @vowels, [ $i, $chars[$i], is_upper($chars[$i]) ];
60 unshift @indices, $i;
61 }
62 }
63 foreach my $j (0..$#vowels) {
64 my ($old_index, $char, $is_upper) = @{ $vowels[$j] };
65 my $new_index = $indices[$j];
66 my $new_char = $char;
67 if($is_upper) {
68 $new_char = uc($new_char);
69 } else {
70 $new_char = lc($new_char);
71 }
72 $chars[$new_index] = $new_char;
73 }
74 $s = join("", @chars);
75 print "Output: \"$s\"\n";
76
77 }
78
79 sub is_vowel {
80 my $char = shift;
81 my $vowels = { "a" => 1, "e" => 1, "i" => 1, "o" => 1, "u" => 1 };
82 return $vowels->{lc($char)};
83 }
84
85 sub is_upper {
86 my $char = shift;
87 return $char eq uc($char);
88 }