-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacterMultiplier.java
More file actions
74 lines (48 loc) · 1.84 KB
/
Copy pathCharacterMultiplier.java
File metadata and controls
74 lines (48 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package textProcessing;
import java.util.Scanner;
public class CharacterMultiplier {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
String first = input.split("\\s+")[0];
String second = input.split("\\s+")[1];
int result = multiplierMethod(first,second);
System.out.println(result);
}
private static int multiplierMethod(String first, String second) {
int firstLength = first.length();
int secondLength = second.length();
int result = 0;
if(firstLength > secondLength){
for (int i = 0; i < secondLength; i++) {
int numFirst = (int) first.charAt(i);
int numSecond = (int) second.charAt(i);
int number = numFirst * numSecond;
result += number;
}
for (int i = secondLength; i < firstLength; i++) {
int addNumber = (int) first.charAt(i);
result += addNumber;
}
}else if(secondLength > firstLength){
for (int i = 0; i < firstLength; i++) {
int numFirst = (int) first.charAt(i);
int numSecond = (int) second.charAt(i);
int number = numFirst * numSecond;
result += number;
}
for (int i = firstLength; i < secondLength; i++) {
int addNumber = (int)second.charAt(i);
result += addNumber;
}
}else {
for (int i = 0; i < secondLength; i++) {
int numFirst = (int) first.charAt(i);
int numSecond = (int) second.charAt(i);
int number = numFirst * numSecond;
result += number;
}
}
return result;
}
}