PHP face Test-A programming question for a PHP interview

Source: Internet
Author: User
please write a function

The language we work with is PHP, and I hope you can answer it in PHP. If you have any difficulty, you can do it in other languages. (e.g. C #, Java, C + +, Python, Perl, Ruby, etc.)

Requirements

Input parameter – A string that returns a string.
Input is an English sentence, only the English text (A-Z, A-Z), each word only with a space, and no space.
The return is to write each word in alphabetical order, but the order of the word and the case of the letter remain slew.

Example

Input parameters
This is a Apple on EBay

Return
Siht si na elppa no yAbe
(such as ebay because it is the second letter uppercase, the second letter after the write down to keep the uppercase.) )

Reply content:

Please write a function

The language we work with is PHP, and I hope you can answer it in PHP. If you have any difficulty, you can do it in other languages. (e.g. C #, Java, C + +, Python, Perl, Ruby, etc.)

Requirements

Input parameter – A string that returns a string.
Input is an English sentence, only the English text (A-Z, A-Z), each word only with a space, and no space.
The return is to write each word in alphabetical order, but the order of the word and the case of the letter remain slew.

Example

Input parameters
This is a Apple on EBay

Return
Siht si na elppa no yAbe
(such as ebay because it is the second letter uppercase, the second letter after the write down to keep the uppercase.) )

Does that make sense?

function revStr($str){    $str2 = implode(" ", array_map("strrev", explode(" ", $str)));    $len = strlen($str);    while ($len > 0) {        $str2{$len - 1} = ord($str{$len - 1}) < 97 ? strtoupper($str2{$len - 1}) : strtolower($str2{$len - 1});        $len--;    }    return $str2;}$str = 'This is an Apple on eBay';echo revStr($str);// Siht si na Elppa no yAbe

http://segmentfault.com/q/1010000002547110/a-1020000002547831

Use $arr=explode (", $input) to split the string first,
Then turn the array over Array_reverse ()
Then Traverse $arr
Intercept with $a=substr ($arr [0],0,1)

if(preg_match('/^[a-z]+$/', $a)){            echo '小写字母';            $a=strtoupper($a)}elseif(preg_match('/^[A-Z]+$/', $a)){            echo '大写字母';            $a = strtolower($a)}

And then you put it together and you're done.

I also come one, slightly faster than @util:)

Function Revstr (& $str) {$offset = 0;    $isEnd = false;        while (! $isEnd) {$spacePos = Strpos ($str, ' ", $offset);            if ($spacePos = = = False) {$spacePos = strlen ($STR);        $isEnd = true;        } $wordLen = $spacePos-$offset;        $wordLenHalf = $wordLen >> 1;            for ($j = 0; $j < $wordLenHalf; $j + +) {$diff = 0;            $charPos = $offset + $j;            $charPosRev = $spacePos-$j-1;            $charASCII = Ord ($str [$charPos]);            $charASCIIRev = Ord ($str [$charPosRev]); $diff + = ($charASCII <= 90)?            -16:16; $diff + = ($charASCIIRev <= 90)?            16:-16;                if ($diff) {$str [$charPos] = chr ($charASCIIRev + $diff);            $STR [$charPosRev] = Chr ($charASCII-$diff);                }else{$tmp = $str [$charPos];                $STR [$charPos] = $str [$charPosRev];            $STR [$charPosRev] = $tmp;     }        }   $offset + = $wordLen +1; }} $str = ' This is a Apple on EBay '; Revstr ($STR); echo $str;//Siht si na elppa no yAbe
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.