Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //Create a method that takes two strings as arguments and returns the sum of their character codes multiplied
- //(multiply str1.charAt (0) with str2.charAt (0) and add to the total sum). Then continue with the next two characters.
- //If one of the strings is longer than the other, add the remaining character codes to the total sum without
- //multiplication.
- $str=readline();
- //$str="123 522";
- $divided=explode(" ",$str);
- $str1_arr=str_split($divided[0]);;
- $str2_arr=str_split($divided[1]);
- $i=0;
- $len1=count($str1_arr);
- $len2=count($str2_arr);
- $diff=$len1-$len2;
- if($diff>0)
- {
- for($i=0;$i<$diff;$i++)
- {
- $str2_arr[$len2+$i]=1;
- }
- }
- elseif($diff<0)
- {
- for($i=0;$i<abs($diff);$i++)
- {
- $str1_arr[$len1+$i]=1;
- }
- }
- $sum=0;
- for($i=0;$i<count($str2_arr);$i++)
- {
- if(is_string($str1_arr[$i]))
- {
- $str1_arr[$i]=ord($str1_arr[$i]);
- }
- if(is_string($str2_arr[$i]))
- {
- $str2_arr[$i]=ord($str2_arr[$i]);
- }
- $sum+=$str1_arr[$i] *$str2_arr[$i];
- }
- echo $sum;
Add Comment
Please, Sign In to add comment