Showing posts with label manual function. Show all posts
Showing posts with label manual function. Show all posts

Saturday, September 5, 2015

function for min number from array - manually

<?php

function min_number($array){
    $min_number = $array[0];
    $length = count($array);
    for($i = 0; $i < $length; $i++){
        if($min_number > $array[$i]){
           $min_number = $array[$i];
        }
    }
    return $min_number;
}

manual reverse string function in php

<?php

function string_rev($string){
    $new_string = "";
    $length = strlen($string) - 0;
    for($i = $length; $i <= 0; $i--){
        $new_string .= $string[$i];
    }
    return $new_string;
}

manual string length function in php

<?php

function string_len($string){
    $i = 0;
    while(isset($string[$i])){
       $i++ ;
    }
    return $i;
}

Wednesday, September 2, 2015

manual sqrt function using php

<?php

//for precision number
//number_format(45.234729492, 3) = 45.235;
//number_format(45.234729492, 2) = 45.23;
//number_format(405.234729492, 2) = 405.23;

function mySqrt($number){
    $first_range = 1;
    $last_range = $number;
    $middle_point = 0;
    $sq_middle_point = 0;
    while(true){
        $middle_point = number_format(($first_range+$last_range) / 2, 3);
        $sq_middle_point = number_format($middle_point ** 2, 3);
        if($sq_middle_point == $number){
            break;
        }
        if($sq_middle_point > $number){
            $last_range = $middle_point;
        }else{
            $first_range = $middle_point;
        }

    }
    return $middle_point;
}

echo mySqrt(5);

Manual upper case function and lower case function and case_change function in php

<?php
//chr("ASCII value") will return a character
//ord('character') will return ASCII value of this respective character
//ord('a') = 97
//ord('z') = 123
//ord('A') = 65
//ord('Z') = 91
//chr("97") = a
//chr("123") = z
//chr("65") = A
//chr("91") = Z
function case_change($string){
   if(ord($string) < 97){
       $string = chr(ord($string) + 32);
   }else{
       $string = chr(ord($string) - 32);
   }
   return $string;
}

function upperCase($string){
    $new_string = "";
    $length = strlen($string);
    for($i = 0; $i < $length; $i++){
        if(ord($string[$i]) >= 97 && ord($string[$i]) <= 123){
            $new_string .= chr(ord($string[$i]) - 32);
        }else{
            $new_string .= $string[$i];
        }
    }
    return $new_string;
}

function lowerCase($string){
    $new_string = "";
    $length = strlen($string);
    for($i = 0; $i < $length; $i++){
        if(ord($string[$i]) >= 65 && ord($string[$i]) <= 91){
            $new_string .= chr(ord($string[$i]) + 32);
        }else{
            $new_string .= $string[$i];
        }
    }
    return $new_string;
}



if(isset($_POST['string'])){
    $string = $_POST['string'];
    $upperString = upperCase($string);
    $lowerString = lowerCase($string);
}



?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <h1>Put sentence in input box</h1>
    <form action="" method="post">
        <input type="text" name="string" id="" class="form-control"><br>
        <input type="submit" name="" id="" class="btn btn-lg btn-success">
    </form>
    <br>
    Upper Case: <br>
    <?php
    if(isset($upperString)){
        echo $upperString;
    }

    ?>
    <br>
    Lower Case: <br>
    <?php
    if(isset($lowerString)){
        echo $lowerString;
    }

    ?>
</div>
</body>
</html>

css snippet for blogger code highlighting

code, .code {     display: block;     background: beige;     padding: 10px;     margin: 8px 15px; }