Skip to content

Instantly share code, notes, and snippets.

@Bas-Man
Created October 21, 2018 13:05
Show Gist options
  • Save Bas-Man/bfddb506b932693e7da2e6c4e7ec3464 to your computer and use it in GitHub Desktop.
Save Bas-Man/bfddb506b932693e7da2e6c4e7ec3464 to your computer and use it in GitHub Desktop.
Quick subroutine to return the number of vowels in a string. returns -1 if the string is not an ascii string.
sub asciiVowelCount {
my $str = shift;
# return error since this is not ascii character set
return -1 if $str =~ /[^[:ascii:]]/;
my $count = 0;
# Loop over each chracter in $str and increment counter if it is a vowel
for my $char (split //, $str) {
$count++ if $char =~ m/[aiueo]/;
}
return $count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment