Advertisement
meteor4o

JF-TextProcessing-Exercise-02.CharacterMultiplier

Jul 19th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.03 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class CharacterMultiplier {
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner(System.in);
  8.  
  9.         String input = sc.nextLine();
  10.         String first = input.substring(0, input.indexOf(" ")).trim();
  11.         String second = input.substring(input.indexOf(" ") + 1).trim();
  12.  
  13.         int sum = calcSumOfChars(first, second);
  14.         System.out.println(sum);
  15.     }
  16.  
  17.     private static int calcSumOfChars(String first, String second) {
  18.         int sum = 0;
  19.  
  20.         for (int i = 0; i < first.length() && i < second.length(); i++) {
  21.             sum += first.charAt(i) * second.charAt(i);
  22.         }
  23.         if (first.length() > second.length()) {
  24.             for (int i = second.length(); i < first.length(); i++) {
  25.                 sum += first.charAt(i);
  26.             }
  27.         } else {
  28.             for (int i = first.length(); i < second.length(); i++) {
  29.                 sum += second.charAt(i);
  30.             }
  31.         }
  32.         return sum;
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement