[英]How to find the occurrences letters of a string for given number in PHP
I have string $str = 'street';
我有字符串$str = 'street';
and I want to write a function output($str, $n)
我想写一个 function output($str, $n)
Here the $n
is the count for number of occurrences in the given string.这里的$n
是给定字符串中出现次数的计数。 I don't want to use any PHP inbuilt functions like converting it to array first or substr_count
or any other for this and want to do this just with pure for looping if it is possible我不想使用任何 PHP 内置函数,例如先将其转换为数组或substr_count
或任何其他函数,并且如果可能的话,只想使用纯循环来执行此操作
Considering $n as 2, the expected output is:-考虑 $n 为 2,预期的 output 是:-
t -> 2 t -> 2
e -> 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);
So here, I have tried this above code and it works absolutely fine, but here I used the str_split function for this.所以在这里,我已经尝试了上面的代码,它工作得很好,但是在这里我使用了 str_split function 。 I want to avoid its usage and need it to be done with looping only if it's possible.我想避免使用它,并且只有在可能的情况下才需要使用循环来完成它。
I am bad at formatting questions so please spare me on this.我不擅长格式化问题,所以请原谅我。
You can use a for loop instead:您可以改用 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);
If you can't use strlen
you can use while
loop with isset
:如果你不能使用strlen
你可以使用while
循环isset
:
$i = 0;
while(isset($input[$i])) {
if(!isset($outputs[$input[$i]])){
$outputs[$input[$i]] = 1;
} else {
$outputs[$input[$i]] += 1;
}
$i++;
}
Removing both the str_split
and the str_replace()
functions can be done by using the fact that a string can be accessed as an array, so loop over the string and pick out each character by it's position in the string...删除str_split
和str_replace()
函数可以通过使用字符串可以作为数组访问的事实来完成,因此循环字符串并通过字符串中的 position 选择每个字符...
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";
}
}
}
to remove the strlen()
, just check if each element of the string is set...要删除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";
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.