记一次面试笔试题

  • 面试题是全英文的,但单词都比较熟悉算是好理解,而且有案例。所以,稍微细心一点是完全可以避免的,这里写下题目,以及解题过程。

  • 题:

If you think the description is incomplete and affect the answers in any way, please make appropriate assumption and describe it in the answer·         
We are a PHP shop and prefer you answering in PHP. 
But you might answer in any other languages in case you are not familiar with it, we accept answers in C#, Java, Javascript, Python, Perl, C++, Ruby, etc.
Problem Statement
The input is an English sentence as a string, we need a function - “function convert($input)” that can transform it as described below and return a new string.
The sentence would contain only alphabet (a-z and A-Z) and space, each word would be separated by exactly one space. There would be no spaces before and after the sentence.
Please return the string with each word spelled in reverse, however, the position of the capitalization of each letter should stay the same for each word.
For example:
Input: Many people spell MySQL incorrectly
Output: Ynam elpoep lleps LqSYM yltcerrocni
  • 解:
$string = 'Many people spell MySQL incorrectly';
function myStrrev($string){
    $paterrn = '/^([a-zA-Z]){1,100}((\s){1}([a-zA-Z]){1,100})*$/'; //假设一个单词最大范围1-100
    if(preg_match($paterrn,$string)) {
        $data = explode(' ',$string); 
        foreach($data as $key => $value){
            $newValue = strtolower(strrev($value));
            for($i=0;$i<mb_strlen($value);$i++) {
                $str = (mb_substr($value, $i, 1));
                if(preg_match('/[A-Z]/',$str)){
                    $newValue[$i] = strtoupper($newValue[$i]);
                }
            }
            $data[$key] = $newValue;
        }
        echo implode(' ',$data);
     } else {
        echo '句子不符合要求';
     }
}
echo myStrrev($string);
  • 不管是开发也还,还是做题也好。正确理解题目永远是最关键的,心里毒打自己一百遍!!!
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值