-1

Sorry if this question already exists. I have a question can you help me. Only use strlen function and array. Can you make programming count number of character occurrences in a string with PHP (Character not repeat). Similar to create a new array_count_values function that available in php. Eg:

$input = 'lorem lis pum';
Output: 
l appeared 2 times;
o appeared 1 times;
r appeared 1 times;
...

This is my code when using available function array_count_values and it's worked.

$a = str_split($str, 1);
foreach (array_count_values($a) as $key => $value) {
    if ($key === ' ') {
        $key = '_';
    }
    echo $key.'appeared '.$value.' times<br>';
}

Thanks for read.

2
  • I'm curios to know why you need to create a paradigm that is already exist ? does there another approach you want to achieve ? and for that you need to create a new method for that? Commented May 22, 2017 at 8:55
  • @hassan I want to learn more about this. and maybe I will make a new method in the future. :)) Commented May 22, 2017 at 9:00

4 Answers 4

3

This is one way to do it. I'm sure there's even more concise ways to do it.

$input = 'lorem lis pum';

for( $i = 0, $ilen = strlen( $input ), $result = array(); $i < $ilen; $i++ ) {
  $chr = $input[ $i ];
  // substituting the space with an underscore is not actually necessary
  if( $chr === ' ' ) {
    $chr = '_';
  }
  $result[ $chr ] = isset( $result[ $chr ] ) ? $result[ $chr ] + 1 : 1;
}

var_dump( $result );

You can view this example online.

Sign up to request clarification or add additional context in comments.

1 Comment

while(isset($input[$i++]))
2

You can try this, hope its help :)

<?php
$input = 'lorem lis pum';

function outputs($input)
{
    $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)
    {
        echo $char . ' appeared ' . $number . ' times;<br />';
    }
}

outputs($input);
?>

or simple version:

<?php
$input = 'lorem lis pum';

function outputs($input)
{
    //suggest remove all spaces first
    $input = str_replace(' ', '', $input);

    $chars = str_split($input);

    foreach (array_count_values($chars) as $key => $value)
    {
        echo $key. ' appeared '.$value.' times<br>';
    }
}

outputs($input);
?>

Comments

0

Try use function count_chars

foreach (count_chars($str, 1) as $key => $value) {
    echo chr($key).' appeared '.$value.' times<br>';
}

1 Comment

thanks @Timur but I don't want to use availiable function.
-1
you should try this...

if "strlen()" also not allowed then use another for loop to find the length



 $str = "hippopotamous";
    function countValue($str){
        $arr = [];
        $len = strlen($str);
        for($k = 0; $k<$len; $k++){
            $arr[$str[$k]] =0;
        }
        for($i=0; $i<$len; $i++){
                if($arr[$str[$i]]){
                    $arr[$str[$i]] +=1;
                }else{
                     $arr[$str[$i]] =1;
                }
        }
        return $arr;
    } 
    print_r(countValue($str));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.