简体   繁体   中英

How to find the occurrences letters of a string for given number in PHP

I have string $str = 'street'; and I want to write a function output($str, $n)

Here the $n is the count for number of occurrences in the given string. 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

Considering $n as 2, the expected output is:-

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);

So here, I have tried this above code and it works absolutely fine, but here I used the str_split function for this. 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:

<?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 :

$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...

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...

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";
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM