如何在php中反转字符串的元音

时间:2018-06-14 10:26:29

标签: php

我想在PHP中反转字符串的元音。我在下面的代码中写了这个,但结果集是空的。

示例 -

"你好",返回" holle"

" leetcode",返回" leotcede"

请提前帮助我实现这一目标。

<?php
function reverseVowels($string) {

    $result = '';

    $string = array();
    $vowels = array();

    $strlength = count($string);
    for ($i=0; $i < $strlength; $i++) {
        $orig = $strlength[$i];
        $char =  strtolower($strlength[$i]);

        if ($char === 'a' || $char === 'e' || $char === 'i' || $char === 'o' || $char === 'u') {
            array_push($vowels, $orig);
            $orig = null; 
        }

         array_push($string, $orig);

    }
    $new_strlength = count($string);

    for ($i=0; $i < $new_strlength; $i++) {
        if (!$string[$i]) {
            $string[$i] = array_splice($vowels, count($vowels) - 1, 1);
        }
    }

    $result = $string;
    return $result;

}

$res = reverseVowels("hello hello");
print_r($res);

//"hello", return "holle"
//"leetcode", return "leotcede"
?>

5 个答案:

答案 0 :(得分:2)

我稍微改了你的话:

function reverseVowels($string)
{
    $result = '';
    $out    = [];
    $vowels = [];

    $len = strlen($string);

    for ($i=0; $i < $len; $i++) {
        $orig = $string[$i];
        $char = strtolower($string[$i]);
        if (in_array($char, ['a','e','i','o','u'])) {
            $vowels[] = $orig;
            $orig = null; 
        }
        $out[] = $orig;
    }

    for ($i=0, $j=count($vowels)-1; $i < $len; $i++)
        $result .= $out[$i] == null
            ? $vowels[$j--]
            : $out[$i];

    return $result;
}

或者使用数组函数:

function reverse_vowels(string $str)
{
    $vowels       = ['a','e','i','o','u'];
    $replacements = [];
    $chars        = str_split($str);
    $replacements = array_intersect($chars, $vowels);
    $replacements = array_combine(
        array_keys($replacements),
        array_reverse($replacements)
    );
    $chars  = array_replace($chars, $replacements);

    return implode('', $chars);
}

echo reverse_vowels('stackoverflow');

输出:

stockevorflaw

答案 1 :(得分:1)

以下是更改后的工作代码:

function reverseVowels($str) {
  $result = '';
    $string = array();
    $vowels = array();
    $strlength = count($str);
    for ($i=0; $i < strlen($str); $i++) {
        $orig = $str[$i];
        $char =  strtolower($str[$i]);
        if ($char === 'a' || $char === 'e' || $char === 'i' || $char === 'o' || $char === 'u') {
            array_push($vowels, $orig);
            $orig = null; 
        }
         array_push($string, $orig);
    }
    $new_strlength = count($string);
    for ($i=0; $i < count($string); $i++) {
        if (!$string[$i]) {
            $string[$i] = array_splice($vowels, count($vowels) - 1, 1)[0];
        }
    }
    $result = $string;
    return $result;
}
$res = reverseVowels("leetcode");
echo "<pre>";
print_r($res);
echo "</pre>";

答案 2 :(得分:1)

我喜欢功能风格 - 它产生最短的代码。所以这就是:

ip a

输出:

  

Tho qaeck brewn fox跳跃了ovor thi luzy deg

好问题!

答案 3 :(得分:0)

试试这个简单的方法,

%

答案 4 :(得分:0)

这是另一种工作正常的解决方案

function solution($str) {
    $vowels = [];
    $strArr = str_split($str);
    foreach ($strArr as $char) {
      if (in_array($char, ['a','e','i','o','u']) && !in_array($char, $vowels)) {
        $vowels[] = $char;
      }
    }
    $result = "";
    foreach ($strArr as $char) {
      $pos = array_search($char, $vowels);
      if ($pos === false) {
        $result .= $char;
        continue;
      }
      if ($pos == 0) {
        $result .= $vowels[1];
        continue;
      }
      if ($pos == 1) {
        $result .= $vowels[0];
        continue;
      }
      if ($pos % 2 == 0 && array_key_exists(($pos+1), $vowels)) {
        $result .= $vowels[$pos+1];
        continue;
      }
      if ($pos % 2 == 0 && !array_key_exists(($pos+1), $vowels)) {
        $result .= $vowels[$pos];
        continue;
      }
      if ($pos % 2 != 0) {
        $result .= $vowels[$pos-1];
        continue;
      }
    }
    return $result;
}

从我的存储库中提取 https://github.com/sodmond/vowel_reversal