Bit Stuffing Program in c
Implementation
of Bit-Stuffing.
#include<stdio.h>
#include<conio.h>
void
sender(int *);
void
receiver(int *);
int
main()
{
int
a[10],i;
clrscr();
/*Input
string to be sent*/
for(i=0;i<8;i++){
printf("Enter
a[%d]: ",i+1);
scanf("%d",&a[i]);
}
/*Sending
string*/
sender(a);
getch();
return
0;
}
/*Sender
code*/
void
sender(int *a)
{
int
i,j,flag=0,b[10],t=0;
/*Check
if 5 consecutive bits are 1*/
for(i=0;i<8;i++){
if(a[i]==1
&& a[i+1]==1 && a[i+2]==1 && a[i+3]==1 &&
a[i+4]==1){
flag=1;
t=i;
break;
}
}
if(flag==1){
flag=0;
i=t+5;
/*Stuffing
bit*/
for(j=0;j<i;j++)
b[j]=a[j];
b[j]=0;
for(j=i+1;j<9;j++)
b[j]=a[j-1];
b[j]='\0';
printf("Bit
Stuffed\n");
}
/*If
there are no 5 consecutive 1’s (no bit stuffing)*/
else
{
printf("Message
sent: ");
for(i=0;i<8;i++){
printf("%d",a[i]);
}
printf("\n");
/*Call
receiver (bit not stuffed)*/
receiver(a);
return;
}
printf("Message
sent: ");
for(i=0;i<9;i++){
printf("%d",b[i]);
}
printf("\n");
/*Call
receiver (bit stuffed)*/
receiver(b);
}
/*Receiver
code*/
void
receiver(int *b)
{
int
i,j,flag=0,a[10],t=0;
/*Check
for 5 consecutive 1’s*/
for(i=0;i<8;i++){
if(b[i]==1
&& b[i+1]==1 && b[i+2]==1 && b[i+3]==1 &&
b[i+4]==1){
flag=1;
t=i;
break;
}
}
if(flag==1){
flag=0;
i=t+5;
/*Destuff
bit*/
for(j=0;j<i;j++)
a[j]=b[j];
for(j=i;j<8;j++)
a[j]=b[j+1];
printf("Bit
Destuffed\n");
t=0;
}
/*Print
original message (bit destuffing not required*/
else
{
printf("Message
received: ");
for(i=0;i<8;i++){
printf("%d",b[i]);
}
t=1;
}
/*Print
original message (bit destuffing required)*/
if(t!=1){
printf("Message
received: ");
for(i=0;i<8;i++){
printf("%d",a[i]);
}
}
}
Output
Are you eager to enhance your Java programming skills and stay ahead in the competitive IT landscape? Look no further! Our Java Frameworks Training Course in Noida is meticulously crafted to empower you with the latest tools and techniques in Java development. Noida, a thriving IT hub, provides the perfect backdrop for your learning journey.
ReplyDelete