Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class CharacterMultiplier {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         String[] input = scanner.nextLine().split(" ");
  8.         System.out.println(MultiplyCharacters(input[0],input[1]));
  9.     }
  10.     public static long MultiplyCharacters (String firstString,String secondString) {
  11.         char[] firstWord = firstString.toCharArray();
  12.         char[] secondWord = secondString.toCharArray();
  13.         long sum = 0;
  14.         long tempSum = 0;
  15.         int shorterWord = Math.min(firstWord.length, secondWord.length);
  16.         int longestWord = Math.max(firstWord.length, secondWord.length);
  17.  
  18.         for (int i = 0; i < shorterWord; i++)
  19.         {
  20.             tempSum = (int) firstWord[i] * (int) secondWord[i];
  21.             sum += tempSum;
  22.         }
  23.  
  24.         if (firstWord.length < secondWord.length)
  25.         {
  26.             for (int i = shorterWord; i <= longestWord - 1; i++)
  27.             {
  28.                 sum += secondWord[i];
  29.             }
  30.         } else if (firstWord.length > secondWord.length)
  31.         {
  32.             for (int i = shorterWord; i <= longestWord - 1; i++)
  33.             {
  34.                 sum += firstWord[i];
  35.             }
  36.         }
  37.         return sum;
  38.  
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement