-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

4
  • stackoverflow.com/questions/838227/… Commented Jun 30, 2015 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 Commented Jun 30, 2015 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? Commented Jun 30, 2015 at 12:29
  • Use PHP to sort by length but you can't use PHP?? Commented Jun 30, 2015 at 12:31

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

2 Comments

I think you missed that part: without using built in functions
OP didn't really ask for code, but a basic approach description.
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).

2 Comments

without using built in functions
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.
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?

1 Comment

yes exactly this is what i want :) Thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.