desislava_topuzakova

2. Character Multiplier

Nov 12th, 2021
1,413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.72 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.         String[] input = scanner.nextLine().split(" +");
  7.         String first = input[0].trim();
  8.         String second = input[1].trim();
  9.         int sum = 0;
  10.  
  11.         for (int i = 0; i < Math.max(first.length(), second.length()); i++) {
  12.             if(i < first.length() && i < second.length()){
  13.                 sum += first.charAt(i) * second.charAt(i);
  14.             }else if(i < first.length()){
  15.                 sum += first.charAt(i);
  16.             }else{
  17.                 sum += second.charAt(i);
  18.             }
  19.         }
  20.  
  21.         System.out.println(sum);
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment