php_program 4

Practical Name:- Write a PHP script for the following: Design a form to accept a string. Write a function to count the total number of vowels (a,e,i,o,u)from the string. Show the occurrences of each vowel from the string. Check whether the given string is a palindrome or not, without using built-in function.(Use radio buttons and the concept of function. Use ‘include’ construct or require stmt.)

_______________________________________________________________________________

index.php

<!DOCTYPE html>

<html>

<head>

  <title>Vowel Counter and Palindrome Checker</title>

</head>

<body>

  <form action="" method="post">

    Enter a string: <input type="text" name="input_string"><br><br>

    <input type="radio" name="operation" value="count_vowels" checked> Count vowels<br>

    <input type="radio" name="operation" value="check_palindrome"> Check Palindrome<br><br>

    <input type="submit" value="Submit">

  </form>

  <br><br>


<?php


if (isset($_POST['input_string'])) {

    include 'vowel_counter.php';

    include 'palindrome_checker.php';


    $input_string = $_POST['input_string'];

    $operation = $_POST['operation'];


    if ($operation == 'count_vowels') {

        count_vowels($input_string);

    } else {

        check_palindrome($input_string);

    }

}


?>


</body>

</html>

// File: vowel_counter.php


<?php

function count_vowels($input_string) {

    $vowels = array('a', 'e', 'i', 'o', 'u');

    $vowel_count = array('a' => 0, 'e' => 0, 'i' => 0, 'o' => 0, 'u' => 0);


    for ($i = 0; $i < strlen($input_string); $i++) {

        if (in_array($input_string[$i], $vowels)) {

            $vowel_count[$input_string[$i]]++;

        }

    }


    echo "Total number of vowels: " . array_sum($vowel_count) . "<br>";

    echo "Occurrences of each vowel: <br>";

    foreach ($vowel_count as $vowel => $count) {

        echo $vowel . ": " . $count . "<br>";

    }

}


?>

// File: palindrome_checker.php


<?php

function check_palindrome($input_string) {

    $input_string = strtolower($input_string);

    $input_string = preg_replace("/[^a-zA-Z0-9]+/", "", $input_string);


    $reverse_string = strrev($input_string);


    if ($input_string == $reverse_string) {

        echo "The string is a palindrome";

    } else {

        echo "The string is not a palindrome";

    }

}


?>


output


Post a Comment

Previous Post Next Post