Didart

Character Multiplier

Nov 14th, 2021
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.76 KB | None | 0 0
  1. import java.util.Scanner;
  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.  
  9.         String str1 = input[0].trim();
  10.         String str2 = input[1].trim();
  11.  
  12.         int sum = 0;
  13.  
  14.         for (int index = 0; index < Math.max(str1.length(), str2.length()); index++) {
  15.             if (index < str1.length() && index < str2.length()) {
  16.                 sum = sum + str1.charAt(index) * str2.charAt(index);
  17.             } else if (index < str1.length()){
  18.                 sum += str1.charAt(index);
  19.             } else {
  20.                 sum += str2.charAt(index);
  21.             }
  22.         }
  23.         System.out.println(sum);
  24.     }
  25. }
  26.  
Advertisement
Add Comment
Please, Sign In to add comment