Compile a function for programming ). The language we use in our work is PHP. I hope you can answer in PHP. If you have any difficulties, you can use other languages. (For example, C #, Java, C ++, Python, Perl, Ruby, etc.) functions must be as follows: Enter the parameter-a string and return a string. The input is an English sentence, only English
Compile a function for programming ). The language we use in our work is PHP. I hope you can answer in PHP. If you have any difficulties, you can use other languages. (For example, C #, Java, C ++, Python, Perl, Ruby, etc.) functions must be as follows: Enter the parameter-a string and return a string. The input is an English sentence, only English
Programming questions
Compile a function ).
The language we use in our work is PHP. I hope you can answer in PHP. If you have any difficulties, you can use other languages. (Such as C #, Java, C ++, Python, Perl, Ruby, etc)
Function requirements are as follows:
Input parameter-a string to return a string.
The input is an English sentence, only English text (a-z, A-Z), each word is only a space lattice, front and back no space.
The returned result is to reverse the alphabetic order of each word, but the order of words and the uppercase and lowercase positions of letters remain unchanged.
Example:
Input parameters
This is an Apple on eBay
Return
Siht si na Elppa no yAbe
(For example, because eBay is the second letter in upper case, keep the second letter in upper case after being reversed .)
My program:
$value){$str_arr[$key] = getNewStrExtra($value);}$new_str = implode(' ',$str_arr);return $new_str;}function getNewStrExtra($str){$length = strlen($str);for($i = 0;$i<$length;$i++){$extra_arr[] = substr($str, $i , 1);}foreach ($extra_arr as $key => $value) {$ord_v = ord($value);if($ord_v >= 60 && $ord_v <=90){$flag = $key;}}$return_arr = array_reverse($extra_arr);foreach ($return_arr as $key => $value) {$return_arr[$key] = strtolower($value);}if(isset($flag)){$return_arr[$flag] = strtoupper($return_arr[$flag]);}$return_str = implode('', $return_arr);return $return_str;}$str='This is an Apple on eBay';echo getNewStr($str);?>
Hope you can share it in a better and more efficient way!
Original article address: Share a PHP interview program question. Thank you for sharing it with me.