-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q3.java
50 lines (45 loc) · 1.44 KB
/
Q3.java
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
import java.util.*;
import java.io.*;
class Q3 {
public static void main(String[] args) {
try {
File file = new File("Q3inp.txt");
Scanner sc;
sc = new Scanner(file);
String str;
int i = 1, priority_sum = 0;
char found;
while (sc.hasNextLine()) {
str = sc.nextLine();
found = findInString(str);
System.out.println(i + "\t" + found);
// priority of a = 1 and z = 26
// priority of A = 27 and Z = 52
if (found >= 'A' && found <= 'Z') {
priority_sum += (found - 'A' + 27);
} else if (found >= 'a' && found <= 'z') {
priority_sum += (found - 'a' + 1);
}
i++;
}
sc.close();
System.out.println(priority_sum);
} catch (FileNotFoundException e) {
System.out.println("File not found");
}
}
static char findInString(String str) {
char c = ' ';
int l = str.length();
//check if any character is present in both halves of the string
for (int i = 0; i < l / 2; i++) {
for (int j = l / 2; j < l; j++) {
if (str.charAt(i) == str.charAt(j)) {
c = str.charAt(i);
break;
}
}
}
return c;
}
}