CN Experiment_3
CN Experiment_3
AIM: Write a program for demonstrating Bit and Byte Stuffing at Data Link Layer.
BIT STUFFING
#include <stdio.h>
#include <string.h>
// Function for bit stuffing
void bitStuffing(int N, int arr[])
{
// Stores the stuffed array
int brr[30];
// Variables to traverse arrays
int i, j, k;
i = 0;
j = 0;
// Loop to traverse in the range [0, N)
while (i < N) {
// If the current bit is a set bit
if (arr[i] == 1) {
// Stores the count of consecutive ones
int count = 1
// Insert into array brr[]
brr[j] = arr[i];
// Loop to check for
// next 5 bits
for (k = i + 1;
arr[k] == 1 && k < N && count < 5; k++) {
j++;
brr[j] = arr[k];
count++;
// If 5 consecutive set bits
// are found insert a 0 bit
if (count == 5) {
j++;
brr[j] = 0;
}
i = k;
}
}
// Otherwise insert arr[i] into
// the array brr[]
else {
brr[j] = arr[i];
}
i++;
j++;
}
// Print Answer
for (i = 0; i < j; i++)
printf("%d", brr[i]);
}
// Driver Code
int main()
{
int N = 6;
int arr[] = { 1, 1, 1, 1, 1, 1 };
bitStuffing(N, arr);
return 0;
}
OUTPUT:
BYTE STUFFING
#include <stdio.h>
#include <string.h>
void main()
{
char frame[50][50], str[50][50];
char flag[10];
strcpy(flag, "flag");
char esc[10];
strcpy(esc, "esc");
int i, j, k = 0, n;
strcpy(frame[k++], "flag");
printf("Enter length of String : \n");
scanf("%d", &n);
printf("Enter the String: ");
for (i = 0; i <= n; i++)
{
gets(str[i]);
}
printf("\nYou entered :\n");
for (i = 0; i <= n; i++)
{
puts(str[i]);
}
printf("\n");
for (i = 1; i <= n; i++)
{
if (strcmp(str[i], flag) != 0 && strcmp(str[i], esc) != 0)
{
strcpy(frame[k++], str[i]);
}
else
{
strcpy(frame[k++], "esc");
strcpy(frame[k++], str[i]);
}
}
strcpy(frame[k++], "flag");
printf("------------------------------\n\n");
printf("Byte stuffing at sender side:\n\n");
printf("------------------------------\n\n");
for (i = 0; i < k; i++)
{
printf("%s\t", frame[i]);
}
}
OUTPUT: