0

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.

Kendrick
  • 43
  • 5
  • 1
    So, what exactly is stopping you from trying to do this? Please do make an attempt, and then let us know what specifically you get stuck on. Also, please clarify if you want letters that have _exactly_ the input count, or _at least_ the input count. – Patrick Q Oct 15 '19 at 18:35
  • Also missing are expected results showing different inputs. For example, what about `output('stress', 2)`? Would that show `s` or not? – waterloomatt Oct 15 '19 at 18:41
  • @PatrickQ Please find the updated code where I had to use str_split php function which I want to avoid and yes I want the letters repeating exactly same as the input count. – Kendrick Oct 15 '19 at 18:46
  • @Kendrick Are you ruling out using even `strlen()`? What about `isset()`? These are also "built-in" functions, and yet are likely to be important to your task. – Patrick Q Oct 15 '19 at 18:49

2 Answers2

1

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++;
}
Felippe Duarte
  • 14,901
  • 2
  • 25
  • 29
1

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";
        }
    }
}
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55