Simulate Cyclic Redundancy Check (CRC) error detecton algorithm for noisy channel . Decoder & Encoder in Computer Network -KML Tutorial.
Aim:- Simulate Cyclic Redundancy Check (CRC) error detecton algorithm for noisy channel.
Program:-
A.
- #include<stdio.h>
- #include<string.h>
- void XOR(char a[],char b[]);
- int main(){
- char divisor[10]="1011";
- char dataword[10];
- char codeword[20];
- printf("Enter a codeword:");
- scanf("%s",codeword);
- int n=strlen(codeword);
- int k=strlen(divisor);
- int q=n-k+1; //databit
- int m=k-1;
- int i,j;
- char temp[k];
- int end=q+1;
- for(i=0;i<end;i++){
- for(j=0;j<k;j++){
- if(codeword[i]=='0'){
- i++;
- j--;
- continue;
- }
- temp[j]=codeword[i+j];
- }
- temp[k]='\0';
- if(strlen(temp)<k)
- break;
- XOR(temp,divisor);
- for(j=0;j<k;j++){
- codeword[i+j]=temp[j];
- }
- }
- if(strlen(temp)>0){
- printf("Invalid codeword");
- }else{
- printf("Valid codeword");
- }
- //printf("The codeword of dataword %s is %s\n",dataword,codeword);
- return 0;
- }
- void XOR(char a[],char b[]){
- int i=0;
- while(b[i]!='\0'){
- if(a[i]==b[i])
- a[i]='0';
- else
- a[i]='1';
- i++;
- }
- }
B.
- #include<stdio.h>
- #include<string.h>
- void XOR(char a[],char b[]);
- int main(){
- char divisor[10]="1011";
- char dataword[10];
- printf("Enter a dataword:");
- scanf("%s",dataword);
- int k=strlen(divisor);
- int q=strlen(dataword);
- int m=k-1;
- char n=q+m;
- char codeword[n];
- strcpy(codeword,dataword);
- int i,j;
- for(i=0;i<m;i++){
- codeword[q+i]='0';
- }
- codeword[n]='\0';
- char temp[k];
- int end=q+1;
- for(i=0;i<end;i++){
- for(j=0;j<k;j++){
- if(codeword[i]=='0'){
- i++;
- j--;
- continue;
- }
- temp[j]=codeword[i+j];
- }
- temp[k]='\0';
- if(strlen(temp)<k)
- break;
- XOR(temp,divisor);
- for(j=0;j<k;j++){
- codeword[i+j]=temp[j];
- }
- }
- strcpy(codeword,dataword);
- strcat(codeword,temp);
- printf("The codeword of dataword %s is %s\n",dataword,codeword);
- return 0;
- }
- void XOR(char a[],char b[]){
- int i=0;
- while(b[i]!='\0'){
- if(a[i]==b[i])
- a[i]='0';
- else
- a[i]='1';
- i++;
- }
- }
Comments
Post a Comment