Bit Stuffing Program in C
Here, you will learn about Bit Stuffing and get the example code of the Bit Stuffing Program in C programming language.
What is Bit Stuffing
The Bit stuffing method is used in data communication to enable reliable and accurate data delivery between devices. It is used to identify flag characters or data that have been corrupted or lost. This is also called bit-oriented framing.
Bit stuffing is used in various networking protocols, such as HDLC (High-level Data Link Control), to ensure reliable data transmission.
Algorithm of Bit Stuffing
Step 1: Start the program.
Step 2: Include all the header files.
Step 3: Declare two file pointers so that the input and output files may be opened in read-only and write-only, respectively.
Step 4: Read contents from input file.
Step 5: If the bit is 1, look for four consecutive ones.
Step 6: If so, insert a bit 0 there (i.e., after five consecutive 1s).
Step 7: Open the output file, then print the string that has been stuffed.
Step 8: End the program.
Example Code of Bit Stuffing Program in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | //Bit Stuffing Program in C # include<stdio.h> # include<conio.h> # include<string.h> main() { char a[50],b[50]=""; int i,j,c=0,l; printf("\n Enter a String"); gets(a); strcat(b,"01111110"); for(i=0,j=8;a[i]!='\0';i++) { if(a[i]=='1') c++; else c=0; if(c==5) { b[j++]=a[i]; b[j++]='0'; c=0; } if(c==5) { b[j++]=a[i]; b[j++]='0'; c=0; } else b[j++]=a[i]; } b[j]='\0'; strcat(b,"01111110"); puts("\nString after Bit Stuffing is performed: "); puts(b); l=strlen(b); for(i=0,j=8;j<l-8;i++,j++) { if(b[j]=='1') c++; else c=0; if(c==5) { a[i]=b[j]; j++; c=0; } else a[i]=b[j]; } a[i]='\0'; puts("\n\nString after De-Stuffing is performed: "); puts(a); getch(); } |
Output :-
Enter a String: 01101111110111110
String after Bit Stuffing is perfumed: 0111111001101111101101111101001111110
String after De-Stuffing is performed: 0110111111101111110