CRC
#include <stdio.h>
#include <string.h>
int main()
{
char msg[50], cpy[50];
int flag = 1;
printf("Enter message: ");
scanf("%s", msg);
char di[10];
printf("Enter divisor: ");
scanf("%s", di);
// calculating no. of bits in the message
int len = strlen(msg);
// appending k-1 '0' bits to the msg
int dlen = strlen(msg) + strlen(di) - 1;
for (int i = len; i < dlen; i++)
{
msg[i] = '0';
}
msg[dlen] = '\0';
printf("message after append: ");
printf("%s \n", msg);
// creating a copy of the msg string and storing it in new string "cpy".
strcpy(cpy, msg);
char qu[10], rem[10];
len = strlen(msg) - strlen(di) + 1;
// Performing Modulo-2 binary division
for (int i = 0; i < len; i++)
{
if (cpy[i] == '1')
{
qu[i] = '1';
for (int j = 0; j < strlen(di); j++)
{
/XOR ADDITION logic
if both bits are same then '0',else '1'/
if (cpy[i + j] == di[j])
cpy[i + j] = '0';
else
cpy[i + j] = '1';
}
}
else
{
qu[i] = '0';
for (int j = 0; j < strlen(di); j++)
{
if (cpy[i + j] == '0')
cpy[i + j] = '0';
else
cpy[i + j] = '1';
}
}
}
qu[len] = '\0';
for (int i = len; i < dlen; i++)
{
msg[i] = cpy[i];
}
printf("\nSENDER: \n");
printf("Remainder: ");
printf("%s \n", cpy);
printf("Quotient: ");
printf("%s \n", qu);
// This message is basically Remainder + initial msg
printf("Message to be sent: ");
printf("%s \n", msg);
printf("Enter the reciever data with parity bits:");
scanf("%s", cpy);
// Performing Modulo-2 binary division
for (int i = 0; i < len; i++)
{
if (cpy[i] == '1')
{
qu[i] = '1';
for (int j = 0; j < strlen(di); j++)
{
if (cpy[i + j] == di[j])
cpy[i + j] = '0';
else
cpy[i + j] = '1';
}
}
else
{
qu[i] = '0';
for (int j = 0; j < strlen(di); j++)
{
if (cpy[i + j] == '0')
cpy[i + j] = '0';
else
cpy[i + j] = '1';
}
}
}
qu[len] = '\0';
printf("\nRECEVIER: \n");
printf("Remainder: ");
printf("%s \n", cpy);
printf("Quotient: ");
printf("%s \n", qu);
// finally checking if we can find any bit other than 1 in the remainder on RECEIVER SIDE
for (int i = 0; i < dlen; i++)
{
if (cpy[i] == '1')
{
flag = 0;
break;
}
}
if (flag == 1)
printf("\nSuccessful!!!,NO changes detected.\n");
else
printf("\nError,changes detected.\n");
return 0;
}