-
面試題是全英文的,但單詞都比較熟悉算是好理解,而且有案例。所以,稍微細心一點是完全可以避免的,這裡寫下題目,以及解題過程。
-
題:
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);
複製程式碼
- 不管是開發也還,還是做題也好。正確理解題目永遠是最關鍵的,心裡毒打自己一百遍!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!