-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCyclicRedundancyCheck.java
More file actions
58 lines (57 loc) · 1.39 KB
/
CyclicRedundancyCheck.java
File metadata and controls
58 lines (57 loc) · 1.39 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
import java.util.*;
class CRC{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter Generator : ");
String gen = sc.nextLine();
System.out.print("Enter the data : ");
String data = sc.nextLine();
String code = data;
for(int i = 1; i < gen.length(); i++)
code += "0";
code = data + divide(code, gen);
System.out.println("The transmitied CodeWord is " + code);
System.out.print("Enter the received CodeWord : ");
String rec = sc.nextLine();
if(Integer.parseInt(divide(rec, gen)) != 0)
{
System.out.println("Data Contains Errors!!!");
}
else
{
System.out.println("No Errors!!!");
}
}
static String divide(String data, String gen)
{
int p = gen.length();
int n = gen.length();
String res = data.substring(0, p);
String ans = "";
for(int i = 0; i < p; i++)
{
if(res.charAt(i) == gen.charAt(i))
ans += "0";
else
ans += "1";
}
while(p < data.length())
{
if(ans.charAt(0) == '0')
{
ans = ans.substring(1, n) + String.valueOf(data.charAt(p));
p++;
}
res = ans;
ans = "";
for(int i = 0; i < gen.length(); i++)
{
if(res.charAt(i) == gen.charAt(i))
ans += "0";
else
ans += "1";
}
}
return ans.substring(1, ans.length());
}
}