-3

I have been asked a question in an interview to sort a string by length of its words in php without using built in functions.No idea how to do this. Can somebody help me with this?

String: Sort a string by length of its words

Thanks in advance

  • http://stackoverflow.com/questions/838227/php-sort-an-array-by-the-length-of-its-values –  Jun 30 '15 at 12:25
  • Why this question has been voted down? If you can make a logic then help me out otherwise its a genuine question and i did not find it anywhere – user3541020 Jun 30 '15 at 12:25
  • That's a somewhat vague question. If *all* "built in functions" were prohibited, wouldn't that also exclude `strlen()`? And is it really just one string with words in it, or rather an array of strings? – mario Jun 30 '15 at 12:29
  • Use PHP to sort by length but you can't use PHP?? – PHPhil Jun 30 '15 at 12:31

3 Answers3

1
 $array = array('harish', 'mohan', 'jaideep', 'hari');

    for ($i = 1; $i < count($array); $i++) {
        for ($j = $i; $j > 0; $j--) {
            if (strlen($array[$j]) < strlen($array[$j - 1])) {

                $tmp = $array[$j];
                $array[$j] = $array[$j - 1];
                $array[$j - 1] = $tmp;
            }
        }
    }

    var_dump($array);
Double H
  • 4,120
  • 1
  • 15
  • 26
0

you could try this:

$string = "this is my test string";
$array = explode(" ", $string);
$result = array();
foreach ($array as $key => $string){
    $counter = 0;
    for($i = 0;;$i++){
        if (!isset($string[$i])){
            break;
        }
        $counter++;
    }
    $result[$counter][] = $string;
}

This splits your string and puts it into an array, where the keys are the counted characters. The problem now is, that you need to sort the array by the keys, which can be acquired by using ksort. I do not know if you may use it, if not, refer to this answer (use of sort) or this answer (no sort), this should do the trick (though I didn't test it).

Community
  • 1
  • 1
Tobias Weichart
  • 359
  • 1
  • 13
  • **without** using built in functions – PHPhil Jun 30 '15 at 15:14
  • the task was to sort the words by length, so i guess that the built in functions refer to sorting and array functions. In the example above only explode and isset are used, which should be fine. – Tobias Weichart Jun 30 '15 at 15:22
0

This is the solution that I propose. I added some comments.

<?php

/* First part split the string in their words and store them in an array called $words. 
* The key is the length of the word and the value is an array with all the words having the same length as the key.
* e.g
* Array
(
    [4] => Array( [0] => Sort )
    [1] => Array( [0] => a )
    [6] => Array( [0] => string, [1] => length)
    [2] => Array( [0] => by, [1] => of)
    [3] => Array( [0] => its )
)
*/
$str = "Sort a string by length of its words";

$word = '';
$i = 0;
$word_length = 0;
$words = [];
$max = -1;

while( isset($str[$i]) ) {  
    if( $str[$i] !== ' ' ){
        $word .= $str[$i];
        $word_length++;     
    }else{
        //This is going to save the size of the longhest word in the array:     
        $max = ($word_length > $max) ? $word_length : $max; 
        //store the
        $words[$word_length][] = $word;                     
        $word = '';
        $word_length = 0;
    }
    $i++;
}

//print_r($words); // uncomment this if you wanna see content of the array.


//The if-condition is for ascending order or descending order.
$order = "DESC"; // "ASC" | "DESC"
$res = '';
if( $order === "DESC") {
    for( $i = $max; $i>=0 ; $i--) {
        if( ! isset($words[$i]) ) { continue; } 
        foreach($words[$i] as $word){
            $res .= $word . ' ';
        }               
    }
}else {
    //ascending order:
    for( $i = 0; $i<=$max; $i++) {
        if( ! isset($words[$i]) ) { continue; } 
        foreach($words[$i] as $word){
            $res .= $word . ' ';
        }               
    }
}

echo $res . "\n";
?>

Is this what you want?

Note: isset, echo, print, etc are PHP language constructs whereas print_r(), strlen(), etc. are built in functions. If you have doubts, you can see what's the difference in this post. What is the difference between a language construct and a "built-in" function in PHP?

Community
  • 1
  • 1
Emiliano Sangoi
  • 921
  • 10
  • 20