Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.65 KB | None | 0 0
  1. <?php
  2. $strings = array("Austin is the capital of Texas.",
  3.                  "There are 50 states in the United States of America",
  4.                  "There is no better joke about muffins and an oven than this one:",
  5.                  "There are two muffins are in an oven...",
  6.                  "One says to the other: God it is hot in here",
  7.                  "The other one replies: Oh no... It's a talking muffin");
  8.  
  9.  
  10. // #1
  11. // write a function that calculates and returns the number of alphanumeric
  12. // characters in a string.
  13.  
  14.     function isAlphaNumeric ($byte_value) {
  15.  
  16.         if (($byte_value >= ord ('a') && $byte_value <= ord ('z')) ||
  17.             ($byte_value >= ord ('A') && $byte_value <= ord ('Z')) ||
  18.             ($byte_value >= ord ('0') && $byte_value <= ord ('9'))) {
  19.  
  20.             return TRUE;
  21.         } else {
  22.  
  23.             return FALSE;
  24.         }
  25.  
  26.     }
  27.  
  28.     function numAlphaNumericChars ($instr) {
  29.  
  30.         $chararr = count_chars ($instr, 1);
  31.         foreach ($chararr as $ordval => $count) {
  32.  
  33.             if (isAlphaNumeric ($ordval)) {
  34.  
  35.                 $sum += $count;
  36.             }
  37.         }
  38.  
  39.         return $sum;
  40.     }
  41.    
  42.     $tststr = "aA56%^&hhP9b";  // contains 9 alpha chars
  43.     $cnt = numAlphaNumericChars ($tststr);  
  44.    
  45.     echo "\nNumber of Alphanumeric chars in '$tststr' is $cnt\n\n";
  46.  
  47.  
  48.  
  49. // #2
  50. // write a function that calculates and returns how many words are in a string
  51.  
  52.     function numWords ($instr) {
  53.  
  54.         $count = str_word_count ($instr);
  55.         return $count;
  56.     }
  57.  
  58.     $tststr = "Blenders make soup--frog soup is yummy.";
  59.     $cnt = numWords ($tststr);
  60.     echo "Number of words in '$tststr' is $cnt\n\n";
  61.  
  62.  
  63. // #4
  64. // calculate how many capital letters are contained in the array of sample strings
  65.  
  66.  
  67.     function isCapitalLetter ($byte_value) {
  68.  
  69.         if ($byte_value >= ord ('A') && $byte_value <= ord ('Z')) {
  70.  
  71.             return TRUE;
  72.         } else {
  73.  
  74.             return FALSE;
  75.         }
  76.  
  77.     }
  78.  
  79.     function numCapitalLetters ($strarr) {
  80.  
  81.         foreach ($strarr as $instr) {
  82.  
  83.             $chararr = count_chars ($instr, 1);
  84.             foreach ($chararr as $ordval => $count) {
  85.    
  86.                 if (isCapitalLetter ($ordval)) {
  87.    
  88.                     $sum += $count;
  89.                 }
  90.             }
  91.  
  92.         }
  93.         return $sum;
  94.     }
  95.    
  96.     $count = numCapitalLetters ($strings);
  97.     echo "Number of Capital Letters in strings array is: $count\n\n";
  98.  
  99.  
  100. /*  OUTPUT:  
  101. Number of Alphanumeric chars in 'aA56%^&hhP9b' is 9
  102.  
  103. Number of words in 'Blenders make soup--frog soup is yummy.' is 6
  104.  
  105. Number of Capital Letters in strings array is: 13
  106.  
  107. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement