fork download
  1. //Program to add crc check bit
  2.  
  3. #include<stdio.h>
  4. #include<string.h>
  5. #define N strlen(g)
  6.  
  7. char t[28],cs[28],g[]="1101";
  8. int a,e,c;
  9.  
  10. void xor(){
  11. for(c = 1;c < N; c++)
  12. cs[c] = (( cs[c] == g[c])?'0':'1');
  13. }
  14.  
  15. void crc(){
  16. for(e=0;e<N;e++)
  17. cs[e]=t[e];
  18. do{
  19. if(cs[0]=='1')
  20. xor();
  21. for(c=0;c<N-1;c++)
  22. cs[c]=cs[c+1];
  23. cs[c]=t[e++];
  24. }while(e<=a+N-1);
  25. }
  26.  
  27. int main()
  28. {
  29. printf("\nEnter data : ");
  30. scanf("%s",t);
  31. printf("\n----------------------------------------");
  32. printf("\nGeneratng polynomial : %s",g);
  33. a=strlen(t);
  34. for(e=a;e<a+N-1;e++)
  35. t[e]='0';
  36. printf("\n----------------------------------------");
  37. printf("\nModified data is : %s",t);
  38. printf("\n----------------------------------------");
  39. crc();
  40. printf("\nChecksum is : %s",cs);
  41. for(e=a;e<a+N-1;e++)
  42. t[e]=cs[e-a];
  43. printf("\n----------------------------------------");
  44. printf("\nFinal codeword is : %s",t);
  45. printf("\n----------------------------------------");
  46. printf("\nTest error detection 0(yes) 1(no)? : ");
  47. scanf("%d",&e);
  48. if(e==0)
  49. {
  50. do{
  51. printf("\nEnter the position where error is to be inserted : ");
  52. scanf("%d",&e);
  53. }while(e==0 || e>a+N-1);
  54. t[e-1]=(t[e-1]=='0')?'1':'0';
  55. printf("\n----------------------------------------");
  56. printf("\nErroneous data : %s\n",t);
  57. }
  58. crc();
  59. for(e=0;(e<N-1) && (cs[e]!='1');e++);
  60. if(e<N-1)
  61. printf("\nError detected\n\n");
  62. else
  63. printf("\nNo error detected\n\n");
  64. printf("\n----------------------------------------\n");
  65. return 0;
  66. }
  67.  
Success #stdin #stdout 0.02s 1724KB
stdin
101001
stdout
Enter data : 
----------------------------------------
Generatng polynomial : 1101
----------------------------------------
Modified data is : 101001000
----------------------------------------
Checksum is : 001
----------------------------------------
Final codeword is : 101001001
----------------------------------------
Test error detection 0(yes) 1(no)? : 
No error detected


----------------------------------------