1. 程式人生 > >面試題:字串單詞對換,字串排序

面試題:字串單詞對換,字串排序

 

 

        //字串單詞對換
        $str = "str_arr";
        echo $str."\n";
        $arr = explode("_",$str);

        print_r($arr);
        $str = $arr[1]."_".$arr[0];
        echo $str;

die;
        //字串排序(遍歷方式)
        $str = "2548631";
        $arr = str_split($str);
        print_r($arr);
        
$b=1; $len = count($arr); for($i=0;$i<$len;$i++){ for($j=$i+1;$j<$len;$j++){ if($arr[$i]>$arr[$j]){ $tem = $arr[$i]; $arr[$i] = $arr[$j]; $arr[$j] = $tem; }
$b++; } } $str = implode("",$arr); print_r($arr); echo $b; //字串排序(函式方式) $str = "2548631"; $arr = str_split($str); sort($arr); $str = implode("",$arr); echo $str; die;