forked from CodeToExpress/dailycodebase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHammingDistance.java
34 lines (28 loc) · 1.16 KB
/
HammingDistance.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
import java.util.Scanner;
public class HammingDistance {
public static void main (String[] args) {
System.out.println("/* ===== The Hamming Distance Problem ===== */");
// Input the strings
Scanner input = new Scanner(System.in);
System.out.print("Enter the first string: ");
String str1 = input.next();
System.out.print("Enter the second string: ");
String str2 = input.next();
// set distance equal to zero
int distance = 0;
// check whether the strings are equal
if (str1.length() != str2.length()) {
System.out.println("Wrong Input! \"" + str1 + "\" and \"" + str2 + "\" are not of equal length");
System.exit(-1);
}
// Check the different characters at corresponding positions
for (int i=0; i<str1.length(); i++) {
if (str1.charAt(i) != str2.charAt(i)) {
distance++;
}
}
// Print the result and return the hamming distance
System.out.println("The Hamming Distance between the strings \"" + str1 + "\" and \"" + str2 + "\" is = " + distance);
System.exit(0);
}
}