Wild Widow
Wild Widow

Reputation: 2549

string manipulation uppercase and lowercase in PHP

I was asked this question in a recent interview. A string contains a-z, A-Z and spaces. Sort the string so that all lower cases are at the beginning, spaces in the middle and upper cases at the end. Original order among lower and upper cases needs to remain the same. This is what I came up with:

$str = "wElComE to CaLiFOrNiA";

$lowercase ="";
$uppercase="";
$spaces="";

for ($i=0 ; $i <= strlen($str)-1 ; $i++)
{
    if ($str[$i] <= 'z' && $str[$i] >= 'a'  )
    {
        $lowercase .=$str[$i];
    }

    else if (($str[$i] <= 'Z' && $str[$i] >= 'A'))
    {
        $uppercase .=$str[$i];
    }
    else
    {
        $spaces.=$str[$i];
    }
}
echo $lowercase.$spaces.$uppercase;

input: wElComE to CaLiFOrNiA
output: wlomtoairi ECECLFONA

I'm wondering if there are better ways to do this? And there are 2 spaces in the input string, and the output shows only one space. The complexity of this is O(N) right.

Any ideas?

Upvotes: 0

Views: 260

Answers (3)

MagyaDEV
MagyaDEV

Reputation: 375

function sortString($string)
{
    $string_chars = str_split($string);

    $output = ['', '', ''];

    foreach ($string_chars as $char) {
       $output[$char === ' ' ? 1 : ($char === ucfirst($char) ? 2 : 0)] .= $char;
    }

    return implode('', $output);
}

echo sortString("wElComE to CaLiFOrNiA");

Gives wlomtoairi ECECLFONA

Upvotes: 0

mhall
mhall

Reputation: 3701

I think your solution is pretty OK, but if you are into micro-optimization you could make some minor changes. The following is faster than the regular expression solutions above (benchmark run 10000 times) at least on my system:

$str = "wElComE to CaLiFOrNiA";
$lowercase = "";
$uppercase = "";
$spaces    = "";

$end = strlen($str); // Calculate length just once
for ($i = 0; $i < $end; $i++) {
    $c = $str[$i]; // Get $str[$i] once

    // We know there are only A-Z, a-z and space chars so we
    // only need a greater-than check ('a' > 'A' > ' ')
    if ($c >= 'a') {
        $lowercase .= $c;
    } else if ($c >= 'A') {
        $uppercase .= $c;
    } else
        $spaces .= $c;
}
echo $lowercase . $spaces . $uppercase, PHP_EOL;

Upvotes: 0

chris85
chris85

Reputation: 23892

I'm sure there are many ways to do this. You could do it with regular expressions..

$str = "wElComE to CaLiFOrNiA";
preg_match_all('~([a-z])~', $str, $lowercase);
preg_match_all('~([A-Z])~', $str, $uppercase);
preg_match_all('~(\h)~', $str, $spaces);
echo implode('', $lowercase[1]) . implode('', $spaces[1]) . implode('', $uppercase[1]);

Output:

wlomtoairi  ECECLFONA

When you say better are you referring to performance, readability, or something else?

Second regex approach:

$str = "wElComE to CaLiFOrNiA";
echo preg_replace('~[^a-z]~', '', $str) . preg_replace('~[^\h]~', '', $str) . preg_replace('~[^A-Z]~', '', $str);

Upvotes: 1

Related Questions