0% found this document useful (0 votes)
47 views4 pages

20 - CN Lab-4

Uploaded by

Ansh Agrawal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views4 pages

20 - CN Lab-4

Uploaded by

Ansh Agrawal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CN LAB

Practical 4
AIM: Create and apply data framing techniques in c language-
a. Bit Stuffing and De-Stuffing implementation.
b. Byte Stuffing and implementation.

Name - Ansh Agrawal


Roll no. - A2 - 20
Code A]
#include <stdio.h>
#include <string.h>
void bitStuffing(int N, int arr[]) {
int brr[30];
int i, j, k;
i = 0;
j = 0;
while (i < N) {
if (arr[i] == 1) {
int count = 1;
brr[j] = arr[i];
for (k = i + 1; arr[k] == 1 && k < N && count < 5; k++) {
j++;
brr[j] = arr[k];
count++;
if (count == 5) {
j++;
brr[j] = 0;
}
i = k;
}
} else {
brr[j] = arr[i];
}
i++;
j++;
}

for (i = 0; i < j; i++)


printf("%d", brr[i]);
}
void bitDestuffing(int N, int arr[]) {
int brr[30];
int i, j, k;
i = 0;
j = 0;
int count = 1;

while (i < N) {
if (arr[i] == 1) {
brr[j] = arr[i];
for (k = i + 1; arr[k] == 1 && k < N && count < 5; k++) {
j++;
brr[j] = arr[k];
count++;
if (count == 5) {
k++; // Skip the stuffed bit
}
i = k;
}
} else {
brr[j] = arr[i];
}
i++;
j++;
}

for (i = 0; i < j; i++)


printf("%d", brr[i]);
}

int main() {
int N;
printf("Enter frame size:");
scanf("%d", &N);
int arr[N];
for (int i = 0; i < N; i++) {
scanf("%d", &arr[i]);
}

printf("Bit-stuffed frame: ");


bitStuffing(N, arr);
printf("\n");

printf("Bit-destuffed frame: ");


bitDestuffing(N, arr);

return 0;
}

Code B)
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("\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("Byte stuffing at sender side:\n\n");
for (i = 0; i < k; i++)
{
printf("%s\t", frame[i]);
}
}
Destuffing
#include<stdio.h>
#include<string.h>
int main()
{
char a[10],b[10];
int i,j,n;
printf("\nEnter the frame : \n");
scanf("%s",&a);
n=strlen(a);

for(i=1,j=0;i<(n-1);j++,i++)
{
if(a[i]=='s')
{
b[j]=a[i+1];
i=i+1;

}
else
b[j]=a[i];

}
b[j]='\0';
printf("The Destuffed Frame Is : \n");

printf("%s",b);

You might also like