Skip to content

Commit 6fcbf8b

Browse files
Add files via upload
This is the program for Simulation of an Error Correction Code (Like CRC).
1 parent 9935650 commit 6fcbf8b

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

Crc.java

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import java.io.*;
2+
import java.net.*;
3+
class Crc
4+
{
5+
public static void main(String args[]) throws IOException
6+
{
7+
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
8+
System.out.println("\nEnter Generator: ");
9+
String gen=br.readLine();
10+
System.out.println("\nEnter data: ");
11+
String data=br.readLine();
12+
String code=data;
13+
while(code.length()<(data.length()+gen.length()-1))
14+
code=code+"0";
15+
code=data+div(code,gen);
16+
System.out.println("\nThe transmitted code word is: "+code);
17+
System.out.println("\nPlease enter the received code word: ");
18+
String rec=br.readLine();
19+
if(Integer.parseInt(div(rec, gen))==0)
20+
System.out.println("\nThe received code word contains NO Errors\n");
21+
else
22+
System.out.println("\nThe received code word contains Errors\n");
23+
}
24+
static String div(String num1, String num2)
25+
{
26+
int pointer=num2.length();
27+
String result=num1.substring(0, pointer);
28+
String remainder=" ";
29+
for (int i=0; i<num2.length(); i++)
30+
{
31+
if (result.charAt(i)==num2.charAt(i))
32+
remainder+="0";
33+
else
34+
remainder+="1";
35+
}
36+
while(pointer<num1.length())
37+
{
38+
if(remainder.charAt(0)=='0')
39+
{
40+
remainder=remainder.substring(1, remainder.length());
41+
remainder=remainder+String.valueOf(num1.charAt(pointer));
42+
pointer++;
43+
}
44+
result=remainder;
45+
remainder=" ";
46+
for(int i=0; i<num2.length(); i++)
47+
{
48+
if (result.charAt(i)==num2.charAt(i))
49+
remainder+="0";
50+
else
51+
remainder+="1";
52+
}
53+
}
54+
return remainder.substring(1, remainder.length());
55+
}
56+
}

0 commit comments

Comments
 (0)