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.

  1. #include<stdio.h>
  2. #include<string.h>
  3. void XOR(char a[],char b[]);
  4. int main(){
  5.     char divisor[10]="1011";
  6.     char dataword[10];
  7.     char codeword[20];
  8.     printf("Enter a codeword:");
  9.     scanf("%s",codeword);
  10.     int n=strlen(codeword);
  11.     int k=strlen(divisor);
  12.     int q=n-k+1; //databit
  13.     int m=k-1;
  14.     int i,j;
  15.     char temp[k];
  16.     int end=q+1;
  17.     for(i=0;i<end;i++){
  18.             for(j=0;j<k;j++){
  19.                 if(codeword[i]=='0'){
  20.                     i++;
  21.                     j--;
  22.                     continue;
  23.                 }
  24.                 temp[j]=codeword[i+j];
  25.             }
  26.             temp[k]='\0';
  27.             if(strlen(temp)<k)
  28.                 break;
  29.             XOR(temp,divisor);
  30.             for(j=0;j<k;j++){
  31.                 codeword[i+j]=temp[j];
  32.             }
  33.     }
  34.     if(strlen(temp)>0){
  35.         printf("Invalid codeword");
  36.     }else{
  37.         printf("Valid codeword");
  38.     }
  39.     //printf("The codeword of dataword %s is %s\n",dataword,codeword);
  40.     return 0;
  41. }
  42. void XOR(char a[],char b[]){
  43.     int i=0;
  44.     while(b[i]!='\0'){
  45.         if(a[i]==b[i])
  46.             a[i]='0';
  47.         else
  48.             a[i]='1';
  49.         i++;
  50.     }
  51. }

B.

  1. #include<stdio.h>
  2. #include<string.h>
  3. void XOR(char a[],char b[]);
  4. int main(){
  5.     char divisor[10]="1011";
  6.     char dataword[10];
  7.     printf("Enter a dataword:");
  8.     scanf("%s",dataword);
  9.     int k=strlen(divisor);
  10.     int q=strlen(dataword);
  11.     int m=k-1;
  12.     char n=q+m;
  13.     char codeword[n];
  14.     strcpy(codeword,dataword);
  15.     int i,j;
  16.     for(i=0;i<m;i++){
  17.         codeword[q+i]='0';
  18.     }
  19.     codeword[n]='\0';
  20.     char temp[k];
  21.     int end=q+1;
  22.     for(i=0;i<end;i++){
  23.             for(j=0;j<k;j++){
  24.                 if(codeword[i]=='0'){
  25.                     i++;
  26.                     j--;
  27.                     continue;
  28.                 }
  29.                 temp[j]=codeword[i+j];
  30.             }
  31.             temp[k]='\0';
  32.             if(strlen(temp)<k)
  33.                 break;
  34.             XOR(temp,divisor);
  35.             for(j=0;j<k;j++){
  36.                 codeword[i+j]=temp[j];
  37.             }
  38.     }
  39.     strcpy(codeword,dataword);
  40.     strcat(codeword,temp);
  41.     printf("The codeword of dataword %s is %s\n",dataword,codeword);
  42.     return 0;
  43. }
  44. void XOR(char a[],char b[]){
  45.     int i=0;
  46.     while(b[i]!='\0'){
  47.         if(a[i]==b[i])
  48.             a[i]='0';
  49.         else
  50.             a[i]='1';
  51.         i++;
  52.     }
  53. }


Comments