//this code is designed by codenation.
//3:10 11/28/2020
#include<stdio.h>
void bitstuff();
void destuff();
int main()
{
while(1)
{
int ch;
printf("enter your choice :\n");
printf("1.bit stuffing\n2.destuffing\n3.exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
bitstuff();
break;
case 2:
destuff();
break;
case 3:
return 0;
}
}
}
void bitstuff()
{
int size,n,i,j,k,count=0,stuff=0;
printf("enter the length of data to be send:\n");
scanf("%d",&size);
n=size/5;
int data[size],send_data[size+n];
printf("enter the data to be send:\n");
for(i=0;i<size;i++)
{
printf("enter the %d bit:\n",i+1);
scanf("%d",&data[i]);
}
printf("\nthe data to be send is:\n");
for(i=0;i<size;i++)
{
printf("%d",data[i]);
}
i=0;
while(i<size)
{
for(j=0;j<size+n;j++)
{
send_data[j]=data[i];
if(data[i]==1)
{
count++;
}
if(count==5)
{
send_data[j+1]=0;
count=0;
j++;
stuff++;
}
else if(data[i+1]==0)
{
count=0;
}
i++;
}
}
if(stuff<n)
{
n=n-stuff;
}
if(stuff==0)
{
n=0;
}
printf("\n\nthe data to send after bit stuffing:\n");
for(i=0;i<size+n;i++)
{
printf("%d",send_data[i]);
}
printf("\n\n");
}
void destuff()
{
int size,n,i,j,k,count=0,destuff=0;
printf("enter the length of recieved data:\n");
scanf("%d",&size);
n=size/5;
int data[size],rc_data[size-n];
printf("enter the recieved data:\n");
for(i=0;i<size;i++)
{
printf("enter the %d bit:\n",i+1);
scanf("%d",&data[i]);
}
printf("\nthe data recieved is:\n");
for(i=0;i<size;i++)
{
printf("%d",data[i]);
}
printf("\n");
i=0;
while(i<size)
{
for(j=0;j<size;j++)
{
rc_data[j]=data[i];
if(data[i]==1)
{
count++;
}
if(count==5)
{
i++;
count=0;
destuff++;
}
else if(data[i+1]==0)
{
count=0;
}
i++;
}
break;
}
printf("\n");
if(destuff<n)
{
n=n-destuff;
}
if(destuff==0)
{
n=0;
}
printf("\n\nthe data recieved after destuffing is:\n");
for(i=0;i<size-n;i++)
{
printf("%d",rc_data[i]);
}
printf("\n\n");
}
//this code is developed by codenation.
Comments
Post a Comment