Character Stuffing Program in C
Here, you will know about character stuffing, and get the source code of Character Stuffing Program in C programming language.
What is Character Stuffing?
Character Stuffing is also known as Byte Stuffing and Character Oriented Framing. It is equivalent to bit stuffing, where as operates on bytes whereas bit stuffing operates on bits.
A byte (usually the escape character, ESC) that has a pre-defined pattern is appended to the data section of the frame when any data contains the same pattern as the character in the flag. Whenever the receiver sees an ESC character, it removes it from the data section and treats the next character as data (not as a flag).
But the problem arises when a text contains one or more escape characters and is followed by a flag. To solve this problem, the escape character that is part of the text is marked with another escape character. That is to say, if the ESCAPE character is a part of text, then an extra 1 is added to make it appear that the second 1 is part of the text itself.
* Point to Point Protocol is the byte based protocol.
Algorithm of Character Stuffing (Byte Stuffing)
- Start
- Add DLE STX at the beginning of string
- Check the data to see whether a character is present and to see if the character DLE is contained in the string (for example, DOODLE) Add more DLEs to the string (for example, DOODLEDLE).
- In the string’s last position, send DLE ETX.
- print string
- End
Character Stuffing Program in Computer Networks
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 | //Program Code for Byte Stuffing in C # include<stdio.h> # include<conio.h> # include<string.h> main() { char a[80],b[80]=""; int i,j,l; printf("\n Enter a String"); gets(a); strcat(b,"["); for(i=0,j=1;a[i]!='\0';i++) { if(a[i]=='[') { b[j++]=a[i]; b[j++]='['; } else if(a[i]==']') { b[j++]=a[i]; b[j++]=']'; } else b[j++]=a[i]; } b[j]='\0'; strcat(b,"]"); puts("\nString after Byte Stuffing is performed: "); puts(b); l=strlen(b); for(i=0,j=1;j<l-1;i++,j++) { if(b[j]=='[') { a[i]=b[j]; j++; } else if(b[j]==']') { a[i]=b[j]; j++; } else a[i]=b[j]; } a[i]='\0'; puts("\n\nString after Destuffing is performed: "); puts(a); getch(); } |
Output
Enter a String: abc[de]fgh
String after Byte Stuffing is performed: [abc[[de]]fgh]
String after Destuffing is performed: abc[de]fgh