php 在不使用内置函数的情况下颠倒句子中的单词顺序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9907533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:54:04  来源:igfitidea点击:

Reverse order of words in a sentence without using built in functions

phplogic

提问by Junaid

I had an interview and a question that seems really simple was not clicking in my mind. Just want to get the answer for it because have tried a lot and unable to find solution.

我接受了一次采访,一个看起来很简单的问题并没有在我脑海中浮现。只是想得到它的答案,因为已经尝试了很多但无法找到解决方案。

I have to write a sentence in opposite like

我必须写一个相反的句子

input => My Name Is Junaid
output => Junaid Is Name My

Should not use ANY built-in function of PHP

不应使用 PHP 的任何内置函数

Thanks

谢谢

EDIT

编辑

I did upto this

我做到了这一点

$string = "My Name Is Junaid";
$len = strlen($string);
for($i=$len; $i > 0; $i--){
    echo $string[$i-1];
}

it results

结果

dianuJ sI emaN yM

need some more tweaking

需要更多的调整

回答by Pete

<?php

$str = "My Name is Fred";
$i = 0;
while( $d = $str[$i] )
{
    if( $d == " "){

        $out = " ".$temp.$out;
        $temp = "";
    }else{
        $temp.=$d;

    }
    $i++;
}
echo $temp.$out;
?>

回答by user3599005

Try this:

尝试这个:

$str = 'My name is James'; 
$str_arr = explode(' ',$str); 
$i=0; 
for($i=(count($str_arr)-1);$i>=0;$i--){ 
  echo $str_arr[$i].' '; 
}


回答by Shashank Sharma

<?php
$name = 'shashank is a good boy';
$i = 0;
while($name[$i] != '')
{
    $i++;
}
$len = $i;
for ($j=$len; $j>0; $j--){
    echo $name[$j-1];
}
?>

回答by Nameless

Actually, it's possible, using control structures. Without strlen, too. It will just produce one notice-level error.

实际上,使用控制结构是可能的。也没有strlen。它只会产生一个通知级别的错误。

I am quite sure it can be done nicer, but here is quick solution, without ANY php function, just control structures.

我很确定它可以做得更好,但这里是快速解决方案,没有任何 php 功能,只有控制结构。

$string = 'My Name Is Junaid';
 $i = 0;
 $output = array('');
 $output_index = 1;
 while (true) {
    $char = $string{$i};
    $i++;
    if ($char == ' ') {
        $output[$output_index] = '';
        $output_index++;
    } elseif ($char === '') {
        break;
    } else {
        $output[$output_index - 1] .= $char;
    }
 }

 for ($i = $output_index; $i--; $i >= 0) {
     echo $output[$i];
     echo ' ';
 }

回答by Aman jain

This way you can reduce the number of cycle hence saving will be performance efficient

通过这种方式,您可以减少循环次数,从而节省性能

<?php
$str = "My Name is Aman jain";
$p = explode(' ',$str);

for ($i= (count($p)-1); $i >= 0  ; $i--) { 
   echo $p[$i]. ' ';
}

回答by ansuman chauhan

Please Check This :

请检查这个:

$str = "NAHUAHC PATARP NAMUSNA si eman YM";
$reverseStr = "";
$length = 0;
while (isset($str[$length])) {
    $reverseStr = $str[$length] . $reverseStr;
    $length++;
}
print $reverseStr;
echo "\n";

回答by Kaif Khan

Please take a look the below code: Reverse the string

请看下面的代码:反转字符串

   $str = 'abcdefg';
    $reverseString = '';
    for($i=strlen($str);$i<=strlen($str);$i--){
      $reverseString .= $str[$i];
    if($i==0)
    break;
    }

    echo $reverseString;

The above code output is:

上面的代码输出是:

gfedcba

回答by gauravjeet

Try this :

尝试这个 :

function reverse_string($str) {
  while ($str[$i]) {
    if ($str[$i] != '~') {
      $rstr = $str[$i] . $rstr;
      $str[$i] = '~';
    }
    ++$i;
  }
  return $rstr;
}

回答by Vignesh Chinnaiyan

<?php

$rev = array("vignesh");
    foreach ($rev as $name)
     {
        echo $name[6];
        echo $name[5];
        echo $name[4];
        echo $name[3];
        echo $name[2];
        echo $name[1];
        echo $name[0];
    }

?>

OUTPUT:

输出:

hsengiv

森吉夫

回答by MrTJ

Iterate from the end towards the through the input chars by chars (with []) searching for spaces (or any white chars). If found one, save the position. When found the second one, add the characters between the two positions to the output. At least strlenyou will need, if you can use also substr:

通过字符(使用 [])搜索空格(或任何白色字符)从末尾向输入字符迭代。如果找到,保存位置。当找到第二个时,将两个位置之间的字符添加到输出中。至少strlen你需要,如果你也可以使用substr

<?php
$input = "My Name Is Junaid";
$output = "";
$lastpos = strlen($input);
for ($i = strlen($input) - 1; $i >= 0; $i--) {
  if ($input[$i] == ' ') {
    if (strlen($output) > 0) $output .= ' ';
    $output .= substr($input, $i + 1, $lastpos - $i);
    $lastpos = $i;
  }
}
echo $output;