C program to perform CYCLIC REDUNDANCY CHECK (CRC).
ALGORITHM:
1. 1.Get the input string and the key from the user.
2. Add 0’s to the end of the string based on the length of the key.
3. Perform Binary Division with the string and the key.
4. The reminder is the required CRC which replaces the 0’s in the input string.
5. Transmit the code to the receiver.
6. At the receiving end the key using binary division divides the received message.
7. If reminder is 0 then the received message is correct, otherwise, display the error message.
PROGRAM:
#include<stdio.h>
#include<math.h>
#include<string.h>
char str[20],key[20],str1[20];
void func()
{ int i,j;
char rem[20];
for(i=0;i<strlen(key);i++)
rem[i]=str[i];
while(i<strlen(str))
{ if(rem[0]==key[0])
{ rem[0]='0';
for(j=1;j<strlen(key);j++)
{ if(rem[j]!=key[j])
rem[j]='1';
else
rem[j]='0';
}
}
for(j=0;j<strlen(key)-1;j++)
rem[j]=rem[j+1];
rem[j]=str[i];
rem[j+1]='\0';
i++;
}
if(rem[0]==key[0])
{ rem[0]='0';
for(j=1;j<strlen(key);j++)
{ if(rem[j]!=key[j])
rem[j]='1';
else
rem[j]='0';
}
}
for(j=0;j<strlen(key);j++)
rem[j]=rem[j+1];
rem[j]='\0';
strcat(str1,rem);
printf("\n Decoded string is: %s",str1);
}
main()
{ int i;
printf("\n Enter the coded string:");
scanf("%s",str);
printf("\n Enter the key");
scanf("%s",key);
strcpy(str1,str);
for(i=1;i<strlen(key);i++)
strcat(str,"0");
if(strlen(str)>=strlen(key))
func();
else
printf("\n Invalid key");
}
OUTPUT:
Enter coded string : 11001101
Enter the key : 1101
Decoded string is : 11001101001
No comments:
Post a Comment