# File: count0.pl # Title: Count the occurrences of a letter in the string # Author: Mihaela Malita # What string? Blaise Pascal # What letter? a # Occurences of a = 3 print "What string? "; $s = ; chop($s); print "What letter? "; $c = ; chop($c); # transform string in array @S = split('', $s); # transforms string in array foreach $i ( @S ) { # for every element in array if ($i eq $c) { $k++ }; # increments counter $count } print "\nOccurences of $c in $s = $k"; ######### Again written in another way: $k = 0; # initialize counter @S = split('', $s); # transforms string in array for ( @S ) { # for every element in array if ($_ eq $c) # default loop counter $_ { $k++ }; # increments variable $k } print "\nOccurences of $c in $s = $k"; exit;