Saturday, September 5, 2015

function for lower word between 2 word - basic logic of dictionary

<?php
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;
}

function lower_word($string1, $string2){
    $string1 = lowerCase($string1);
    $string2 = lowerCase($string2);
    $len1 = strlen($string1);
    $len2 = strlen($string2);
    if($len1 < $len2){
        $min_len = $len1;
    }else{
        $min_len = $len2;
    }
    for($i = 0; $i < $min_len; $i++){
        if($string1[$i] > $string2[$i]){
            return $string2;
        }elseif($string1[$i] < $string2[$i]){
           return $string1;
        }
    }
    if($len1 < $len2){
        return $string1;
    }else{
        return $string2;
    }

}
echo lower_word('Bat', 'ball');

No comments:

Post a Comment

css snippet for blogger code highlighting

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