dqeonr8554 2012-11-13 16:33
浏览 25
已采纳

字符串操作,回复回文

Let's categorize this for PHP only.

Ok so I had an interview yesterday for a Senior Software Developer role. I can't remember the question very well but I try to write it as much as I remember.

Question;

Write a function that takes a string as the input and return true if traversed, false if not.

Requirements; (as much as I remember) - Only one input (string) type.
- String param must be passed by reference.
- No variable within the function, he means that if the param is called $str manipulation should be done on that, didn't like me to use a second variable to set reverseStr etc.
- No looping every character in the string, he specifically said not to loop thru every character in the string.
- No Built-in PHP function can be used, i think he was fine with (strlen)

My Answer; (nope he didn't like it)

$str = 'this is testing';
$length = strlen($str);
$reverseStr = '';
for($i=$length-1; $i>=0; $i--) {
 $reverseStr .= $str[$i];
}

So while this does the job somewhat, he didn't like me to go thru every character to get the reverseStr. I am guessing I needed to think about a recursive solution get the last character of the string and index etc.etc. but I am thinking about these as I am writing this, too late!

What you guys thinking?

  • 写回答

3条回答 默认 最新

  • dtxb75622 2012-11-13 16:49
    关注

    the question isn't clear at all.. but if you want to check if the string is a palindrome:

    function is_palindrome ($str){
        if(strlen($str)==0){return true;}
        if($str[0]==$str[strlen($str)-1]){
            return true and is_palindrome(substr($str, 1, strlen($str)-2));
        }else{
            return false;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号