PRACTICAL-8
Aim : Write a program which demonstrates the concept of bit stuffing and
character stuffing.
Hardware Requirement :- Computer or Laptop for run program
Software Requirement:- Turbo c++
Knowledge Requirment :- knowledge require about c programming and concept of bit
stuffing and character stuffing.
Bit Stuffing Program :
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int a[20],b[30],i,j,k,count,n;
printf("Enter frame size (Example: 8):");
scanf("%d",&n);
printf("Enter the frame in the form of 0 and 1 :");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
i=0;
count=1;
j=0;
while(i<n)
{
if(a[i]==1)
{
b[j]=a[i];
for(k=i+1; a[k]==1 && k<n && count<5; k++)
{
j++;
b[j]=a[k];
count++;
if(count==5)
{
j++;
b[j]=0;
}
i=k;
}
}
else
{
b[j]=a[i];
}
i++;
j++;
}
printf("After Bit Stuffing :");
for(i=0; i<j; i++)
printf("%d",b[i]);
getch();
}
Character Stuffing Program :
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
int _tmain(int argc, _TCHAR* argv[])
{
int i=0,j=0,n,pos;
char a[20],b[50],ch;
printf("Enter string\n");
scanf("%s",&a);
n=strlen(a);
printf("\nEnter position : ");
scanf("%d",&pos);
if(pos>n)
{
printf("Invalid position, Enter again : ");
scanf("%d",&pos);
}
printf("Enter the character: ");
ch=getche();
b[0]='d';
b[1]='l';
b[2]='e';
b[3]='s';
b[4]='t';
b[5]='x';
j=6;
while(i<n)
{
if(i==pos-1)
{
b[j]='d';
b[j+1]='l';
b[j+2]='e';
b[j+3]=ch;
b[j+4]='d';
b[j+5]='l';
b[j+6]='e';
j=j+7;
}
if(a[i]=='d' && a[i+1]=='l' && a[i+2]=='e')
{
b[j]='d';
b[j+1]='l';
b[j+2]='e';
j=j+3;
}
b[j]=a[i];
i++;
j++;
}
b[j]='d';
b[j+1]='l';
b[j+2]='e';
b[j+3]='e';
b[j+4]='t';
b[j+5]='x';
b[j+6]='\0';
printf("\n\nFrame after stuffing: ");
printf("%s",b);
getch();
return 0;
}
Questions and Answers
1. What is Byte Stuffing ?
In this method, start and end of frame are recognized with the help of flag bytes.
Each frames starts with and ends with a flag byte. Two consecutive flag bytes
indicate the end of one frame and start of the next one.
Conclusion
In this Practical we know the concept of Bit stuffing and character stuffing
through the program.