Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Read the presentation or search the World Wide Web, and find a PHP rewritten fun

ID: 671177 • Letter: R

Question

Read the presentation or search the World Wide Web, and find a PHP rewritten function named count_chars. Write PHP code that calls the function “count_chars” against the variables below, then test your code and capture and record the results in the template

“aaaaaabbbc”;

“xxxxxxxZzAa”;

“QQQRRrrqq”;

PHP HINT: Use code like I coded below to get & echo counts of the different letters in a string

$data = "AAAAbbbxxx";

foreach (count_chars($data, 1) as $i => $val) {

echo "There were $val instance(s) of "" , chr($i) , "" in the string. ";

}

Above Code Gives you the results:
There were 4 instance(s) of "A" in the string.
There were 3 instance(s) of "b" in the string.
There were 3 instance(s) of "x" in the string.

Explanation / Answer

working code compiled on ideone.

<?php

// your code goes here
$str = "AAAAbbbxxx";
$strArray = count_chars($str,1);

foreach ($strArray as $key=>$value)
{
echo "There were".$value." instance(s) of " ."chr($key)"." in the string";
}
?>

http://ideone.com/nPABGg