Kutov

Untitled

Jun 23rd, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. <?php
  2.  
  3. //Create a method that takes two strings as arguments and returns the sum of their character codes multiplied
  4. //(multiply str1.charAt (0) with str2.charAt (0) and add to the total sum). Then continue with the next two characters.
  5. //If one of the strings is longer than the other, add the remaining character codes to the total sum without
  6. //multiplication.
  7.  
  8. $str=readline();
  9. //$str="123 522";
  10. $divided=explode(" ",$str);
  11. $str1_arr=str_split($divided[0]);;
  12. $str2_arr=str_split($divided[1]);
  13.  
  14. $i=0;
  15. $len1=count($str1_arr);
  16. $len2=count($str2_arr);
  17. $diff=$len1-$len2;
  18.  
  19. if($diff>0)
  20. {
  21. for($i=0;$i<$diff;$i++)
  22. {
  23. $str2_arr[$len2+$i]=1;
  24. }
  25. }
  26. elseif($diff<0)
  27. {
  28.  
  29. for($i=0;$i<abs($diff);$i++)
  30. {
  31. $str1_arr[$len1+$i]=1;
  32. }
  33. }
  34. $sum=0;
  35.  
  36. for($i=0;$i<count($str2_arr);$i++)
  37. {
  38. if(is_string($str1_arr[$i]))
  39. {
  40. $str1_arr[$i]=ord($str1_arr[$i]);
  41. }
  42. if(is_string($str2_arr[$i]))
  43. {
  44. $str2_arr[$i]=ord($str2_arr[$i]);
  45. }
  46. $sum+=$str1_arr[$i] *$str2_arr[$i];
  47. }
  48. echo $sum;
Add Comment
Please, Sign In to add comment