Saturday, August 20, 2016

Count Vowels and Consonants in PERL CGI

A simple program that I wrote using PERL CGI to count the number of vowels and consonants in a given word or a sentence. The code is very simple and easy to understand.

Add me at Facebook my address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com.
 
My mobile number here in the Philippines is 09173084360.






Sample Program Output


Program Listing

vowels_consonants.cgi

#!"D:\xampp\perl\bin\perl.exe"
use CGI;
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
print "Content-type: text/html\n\n";


print "<html>
<style>
body {
     font-family:arial;
     font-size:15;
     color:blue;
    }

</style>
<head><title>Count Vowels and Consonants in PERL CGI</title></head>
<body bgcolor='lightgreen'>
    <h3>Count Vowels and Consonants in PERL CGI</h3>
    <b>Written By: Mr. Jake R. Pomperada,MAED-IT</b>
    <br><br>
    <form action='' method='post'>
        <label>Enter a word or a sentence :<label>
        <input type='text' name='string' size=50/>
        <br><br>
        <input type='submit' value='Ok' />
    </form>
</body>
</html>";  

my $q = new CGI;

if($q->param())
{
    $string = lc($q->param('string'));
    @vowels = ("a","e","i","o","u");
    @consonants=("b","c","d","f","g","h","j","k","l"
                ,"m","n","p","q","r","s","t","v","w","x","y","z");
    $len = length($string);
    $num1 = 0;

  
    @strarr = split(//, $string);
   

    for($i=0; $i<$len; $i++){

        if(in_array(\@vowels, $strarr[$i]))
        {
            $num1++;
        }
      
        if(in_array(\@consonants, $strarr[$i]))
        {
            $num2++;
        }
    }
    print "<br><br>";
    print "<b>===== DISPLAY RESULTS =====</b>";
    print "<br><br>";
    print "The given string or sentence :  <span style='color:red; font-weight:bold;'>".$string."</span></p>";
    print "<p>Number of Vowels : <span style='color:red; font-weight:bold;'>". $num1 ."</span></p>";
    print "<p>Number of Consonants : <span style='color:red; font-weight:bold;'>". $num2 ."</span></p>";

    sub in_array {
         my ($arr,$search_for) = @_;
         return grep {$search_for eq $_} @$arr;
    }
}

No comments:

Post a Comment