-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path07.CRC.java
More file actions
133 lines (116 loc) · 3.62 KB
/
07.CRC.java
File metadata and controls
133 lines (116 loc) · 3.62 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
7. Write a program for error detecting code using CRC-CCITT (16- bits).
*/
import java.util.Scanner;
class CRC {
static String xor(String a, String b) {
StringBuilder stringBuilder = new StringBuilder();
int len = Math.min(a.length(), b.length());
for (int i = 0; i < len; i++) {
if (a.charAt(i) == b.charAt(i)) {
stringBuilder.append('0');
} else {
stringBuilder.append('1');
}
}
return stringBuilder.toString();
}
static String divide(String dividend, String divisor) {
int divisorLength = divisor.length();
int dividendLength = dividend.length();
while (dividendLength >= divisorLength) {
String temp;
if (dividend.charAt(0) == '1')
temp = xor(divisor, dividend.substring(0, divisorLength));
else
temp = dividend.substring(0, divisorLength);
dividend = temp.substring(1) + dividend.substring(divisorLength);
dividendLength -= 1;
}
return dividend;
}
static String generateCodeWord(String message, String generator) {
int msgLength = message.length();
int gtrLength = generator.length();
// Right pad the message String to make total length as (msgLength+gtrLength-1)
// Put the formatted String in new variable
String dividend = String.format("%-" + (msgLength + gtrLength - 1) + "s", message).replace(' ', '0');
String remainder = divide(dividend, generator);
return message + remainder;
}
static boolean checkCodeWord(String codeword, String generator) {
String temp = divide(codeword, generator);
int len = temp.length();
for (int i = 0; i < len; i++) {
if (temp.charAt(i) == '1') {
return false;
}
}
return true;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Generator String");
String generator = scanner.next();
while (true) {
System.out.println("\nMenu");
System.out.println("1. Generate Code Word");
System.out.println("2. Check Code Word");
System.out.println("3. Exit");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("Enter Message");
String message = scanner.next();
String result = generateCodeWord(message, generator);
System.out.println("CodeWord: " + result);
break;
case 2:
System.out.println("Enter Code Word");
String codeWord = scanner.next();
if (checkCodeWord(codeWord, generator)) {
System.out.println("Code Word is Valid");
} else {
System.out.println("Code Word is Invalid");
}
break;
case 3:
System.exit(0);
}
}
}
}
////////////
// Output
//
// Enter Generator String
// 10001000000100001
// Menu
// 1. Generate Code Word
// 2. Check Code Word
// 3. Exit
// 1
// Enter Message
// 1001
// CodeWord: 10011001000100101001
// Menu
// 1. Generate Code Word
// 2. Check Code Word
// 3. Exit
// 2
// Enter Code Word
// 10001000000100001
// Code Word is Valid
// Menu
// 1. Generate Code Word
// 2. Check Code Word
// 3. Exit
// 2
// Enter Code Word
// 10001000000100000
// Code Word is Invalid
// Menu
// 1. Generate Code Word
// 2. Check Code Word
// 3. Exit
// 3