Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $strings = array("Austin is the capital of Texas.",
- "There are 50 states in the United States of America",
- "There is no better joke about muffins and an oven than this one:",
- "There are two muffins are in an oven...",
- "One says to the other: God it is hot in here",
- "The other one replies: Oh no... It's a talking muffin");
- // #1
- // write a function that calculates and returns the number of alphanumeric
- // characters in a string.
- function isAlphaNumeric ($byte_value) {
- if (($byte_value >= ord ('a') && $byte_value <= ord ('z')) ||
- ($byte_value >= ord ('A') && $byte_value <= ord ('Z')) ||
- ($byte_value >= ord ('0') && $byte_value <= ord ('9'))) {
- return TRUE;
- } else {
- return FALSE;
- }
- }
- function numAlphaNumericChars ($instr) {
- $chararr = count_chars ($instr, 1);
- foreach ($chararr as $ordval => $count) {
- if (isAlphaNumeric ($ordval)) {
- $sum += $count;
- }
- }
- return $sum;
- }
- $tststr = "aA56%^&hhP9b"; // contains 9 alpha chars
- $cnt = numAlphaNumericChars ($tststr);
- echo "\nNumber of Alphanumeric chars in '$tststr' is $cnt\n\n";
- // #2
- // write a function that calculates and returns how many words are in a string
- function numWords ($instr) {
- $count = str_word_count ($instr);
- return $count;
- }
- $tststr = "Blenders make soup--frog soup is yummy.";
- $cnt = numWords ($tststr);
- echo "Number of words in '$tststr' is $cnt\n\n";
- // #4
- // calculate how many capital letters are contained in the array of sample strings
- function isCapitalLetter ($byte_value) {
- if ($byte_value >= ord ('A') && $byte_value <= ord ('Z')) {
- return TRUE;
- } else {
- return FALSE;
- }
- }
- function numCapitalLetters ($strarr) {
- foreach ($strarr as $instr) {
- $chararr = count_chars ($instr, 1);
- foreach ($chararr as $ordval => $count) {
- if (isCapitalLetter ($ordval)) {
- $sum += $count;
- }
- }
- }
- return $sum;
- }
- $count = numCapitalLetters ($strings);
- echo "Number of Capital Letters in strings array is: $count\n\n";
- /* OUTPUT:
- Number of Alphanumeric chars in 'aA56%^&hhP9b' is 9
- Number of words in 'Blenders make soup--frog soup is yummy.' is 6
- Number of Capital Letters in strings array is: 13
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement