rjangelov

StringsInPHP - //Problem 2. Count of Letters

Jun 30th, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.88 KB | None | 0 0
  1. //Problem 2.    Count of Letters
  2. //Write a program that reads a list of letters and prints for each letter how many times it appears in the list. The letters should be //listed in alphabetical order. Use the input and output format from the examples below.
  3.  
  4. <!DOCTYPE html>
  5. <html>
  6.     <head>
  7.         <meta charset="UTF-8">
  8.         <title></title>
  9.     </head>
  10.     <body>
  11.         <form method="post" action="CountLetters.php">
  12.             <input type="text" name="str1">
  13.             <input type="submit" value="GO">
  14.         </form>
  15.     </body>
  16. </html>
  17. <?php
  18. $string1=$_POST['str1'];
  19. $arr1 = explode(" ", $string1);
  20. sort($arr1);
  21. for($i=0;$i<=count($arr1)-1;$i++){
  22.     if ($arr1[$i]==$arr1[$i+1]){
  23.         if (!(isset($output[$arr1[$i]]))) {
  24.         $output[$arr1[$i]]=1;
  25.         }
  26.         $output[$arr1[$i]]+=1;
  27.     }
  28. }
  29. foreach($output as $k=>$v){
  30.     echo $k.'->'.$v.'<br>';
  31. }
  32. ?>
Advertisement
Add Comment
Please, Sign In to add comment