【问题标题】:How to find the occurrences letters of a string for given number in PHP如何在PHP中查找给定数字的字符串的出现字母
【发布时间】:2019-10-15 18:33:13
【问题描述】:

我有字符串 $str = 'street'; 我想写一个函数 output($str, $n)

这里的$n 是给定字符串中出现次数的计数。 我不想使用任何 PHP 内置函数,例如先将其转换为数组或 substr_count 或任何其他函数,并且如果可能的话,我只想使用纯 for 循环来做到这一点

考虑 $n 为 2,预期输出为:-

t -> 2

e -> 2

$input = 'street';
$n = 2;

function outputs($input, $n)
{
    $input = str_replace(' ', '', $input);

    $characters = str_split($input);

    $outputs = [];

    foreach ($characters as $char)
    {
        if(!isset($outputs[$char])){
            $outputs[$char] = 1;
        } else {
            $outputs[$char] += 1;
        }
    }

    foreach ($outputs as $char => $number)
    {
        if ($number == $n) {
            echo $char . ' -> ' . $number;
            echo "\n";
        }
    }
}

outputs($input, $n);

所以在这里,我已经尝试了上面的代码,它工作得非常好,但在这里我使用了 str_split 函数。我想避免使用它,并且只有在可能的情况下才需要使用循环来完成。

我不擅长格式化问题,所以请原谅我。

【问题讨论】:

  • 那么,究竟是什么阻止了您尝试这样做?请尝试一下,然后让我们知道您遇到的具体问题。另外,请说明您是否想要完全输入计数或至少输入计数的字母。
  • 还缺少显示不同输入的预期结果。比如output('stress', 2)呢?那会显示s吗?
  • @PatrickQ 请找到我必须使用我想避免的 str_split php 函数的更新代码,是的,我希望字母重复与输入计数完全相同。
  • @Kendrick 你是否排除甚至使用strlen()isset() 呢?这些也是“内置”功能,但可能对您的任务很重要。

标签: php for-loop find-occurrences


【解决方案1】:

您可以改用 for 循环:

<?php

$input = 'street';
$n = 2;

function outputs($input, $n)
{
    $input = str_replace(' ', '', $input);

    $outputs = [];

    for($i = 0; $i < strlen($input); $i++)
    {
        if(!isset($outputs[$input[$i]])){
            $outputs[$input[$i]] = 1;
        } else {
            $outputs[$input[$i]] += 1;
        }
    }

    foreach ($outputs as $char => $number)
    {
        if ($number == $n) {
            echo $char . ' -> ' . $number;
            echo "\n";
        }
    }
}

outputs($input, $n);

如果你不能使用strlen,你可以使用while循环和isset

$i = 0;
while(isset($input[$i])) {
    if(!isset($outputs[$input[$i]])){
        $outputs[$input[$i]] = 1;
    } else {
        $outputs[$input[$i]] += 1;
    }
    $i++;
}

【讨论】:

    【解决方案2】:

    删除str_splitstr_replace() 函数可以通过使用字符串可以作为数组访问的事实来完成,因此循环字符串并根据每个字符在字符串中的位置来选择它。 .

    function outputs($input, $n)
    {
        $outputs = [];
        for ( $i = 0; $i < strlen($input); $i++ )
        {
            $char = $input[$i];
            if ( $char != ' ' ) {
                if(!isset($outputs[$char])){
                    $outputs[$char] = 1;
                } else {
                    $outputs[$char] += 1;
                }
            }
        }
    
        foreach ($outputs as $char => $number)
        {
            if ($number == $n) {
                echo $char . ' -> ' . $number;
                echo "\n";
            }
        }
    }
    

    要删除strlen(),只需检查是否设置了字符串的每个元素...

    function outputs($input, $n)
    {
        $outputs = [];
        $i = 0;
        while ( isset($input[$i]))
        {
            $char = $input[$i];
            if ( $char != ' ' ) {
                if(!isset($outputs[$char])){
                    $outputs[$char] = 1;
                } else {
                    $outputs[$char] += 1;
                }
            }
            $i++;
        }
    
        foreach ($outputs as $char => $number)
        {
            if ($number == $n) {
                echo $char . ' -> ' . $number;
                echo "\n";
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-03-30
      • 2021-11-05
      • 2019-11-04
      • 2017-11-12
      • 2014-08-05
      • 1970-01-01
      • 1970-01-01
      • 2017-05-08
      • 1970-01-01
      相关资源
      最近更新 更多