简体   繁体   中英

How to find the number of vowels in a string using Perl

sub Solution{
    my $n=$_[0];
    my $m=lc $_[1];
    my @chars=split("",$m);

    my $result=0;

    my @vowels=("a","e","i","o","u");


    #OUTPUT [uncomment & modify if required]
    for(my $i=0;$i<$n;$i=$i+1){
        for(my $j=0;$j<5;$j=$j+1){
            if($chars[$i]==$vowels[$j]){
                $result=$result+1;
                last;
            }

        }

    }

    print $result;
}



#INPUT [uncomment & modify if required]
my $n=<STDIN>;chomp($n);
my $m=<STDIN>;chomp($m);


Solution($n,$m);

So I wrote this solution to find the number of vowels in a string. $n is the length of the string and $m is the string. However, for the input 3 nam I always get the input as 3 .

Can someone help me debug it?

== compares numbers. eq compares strings. So instead of $chars[$i]==$vowels[$j] you should write $chars[$i] eq $vowels[$j] . If you had used use warnings; , which is recommended, you'd have gotten a warning about that.

And by the way, there's no need to work with extra variables for the length. You can get the length of a string with length() and of an array for example with scalar() . Also, the last index of an array @a can be accessed with $#a . Or you can use foreach to iterate over all elements of an array.

A better solution is using atr operator which, in scalar context, returns the number of replacements:

perl -le 'for ( @ARGV ) { $_ = lc $_; $n = tr/aeiouy//; print "$_: $n"; }' Use Perl to count how many vowels are in each string
use: 2
perl: 1
to: 1
count: 2
how: 1
many: 2
vowels: 2
are: 2
in: 1
each: 2
string: 1

I included also y , which is sometimes a vowel, see: https://simple.wikipedia.org/wiki/Vowel

Let me suggest a better approach to count letters in a text

#!/usr/bin/env perl
#
# vim: ai:ts=4:sw=4
#

use strict;
use warnings;
use feature 'say';

use Data::Dumper;

my $debug = 0;             # debug flag

my %count;
my @vowels = qw/a e i o u/;

map{
    chomp;
    my @chars = split '';
    map{ $count{$_}++ } @chars;
} <DATA>;

say Dumper(\%count) if $debug;

foreach my $vowel (@vowels) {
    say "$vowel: $count{$vowel}";
}

__DATA__
So I wrote this solution to find the number of vowels in a string. $n is the length of the string and $m is the string. However, for the input 3 nam I always get the input as 3.

Can someone help me debug it?

Output

a: 7
e: 18
i: 12
o: 12
u: 5

Your code is slightly modified form

#!/usr/bin/env perl
#
# vim: ai:ts=4:sw=4
#

use strict;
use warnings;
use feature 'say';

my $input = get_input('Please enter sentence:');

say "Counted vowels: " . solution($input);

sub get_input {
    my $prompt = shift;
    my $input;

    say $prompt;

    $input = <STDIN>;

    chomp($input);

    return $input;
}

sub solution{
    my $str = lc shift;

    my @chars=split('',$str);

    my $count=0;
    my @vowels=qw/a e i o u/;

    map{
        my $c=$_;
        map{ $count++ if $c eq $_} @vowels;
    } @chars;

    return $count;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM