Beispiel #1
0
}
// #2
// write a function that calculates and returns how many words are in a string
//str_word_count();
// #3
// calculate how many spaces are contained in the array of sample strings
function numSpaces($strArray)
{
    foreach ($strArray as $string) {
        for ($c = 0; $c < strlen($string); $c++) {
            $numSpaces += $string[$c] === ' ' ? 1 : 0;
        }
    }
    return $numSpaces;
}
echo numSpaces($strings) . PHP_EOL;
// #4
// calculate how many capital letters are contained in the array of sample strings
function numCaps($strArray)
{
    foreach ($strArray as $string) {
        for ($c = 0; $c < strlen($string); $c++) {
            $numCaps += ctype_upper($string[$c]) ? 1 : 0;
        }
    }
    return $numCaps;
}
echo numCaps($strings) . PHP_EOL;
// #5
// write a function that takes 2 arguments: the first is an array of strings and
// the second is a single string. The function should return the number of times the
Beispiel #2
0
<?php

require_once 'homework/numalphanumeric.php';
require_once 'homework/numspaces.php';
require_once 'homework/numuppercase.php';
require_once 'homework/numwords.php';
require_once 'homework/stringcount.php';
require_once 'homework/wordcounts.php';
require_once 'homework/strings.php';
echo "The answer to #1 is: " . numAlphanumeric($strings) . '<br>';
echo "The answer to #2 is: " . numWords($strings) . '<br>';
echo "The answer to #3 is: " . numSpaces($strings) . '<br>';
echo "The answer to #4 is: " . numUpperCase($strings) . '<br>';
echo "The answer to #5 is: " . stringCount($strings, 'the') . '<br>';
echo "The answer to #6 is: ";
wordCounts($strings);