Implement the data link layer framing methods such as character count, character stuffing and bit stuffing.
Bit Stuffing :
Source Code:
File Name: bit_stuffing.c
#include<stdio.h>
void main() {
int ip_frame[100], op_frame[200];
int i, j = 0, n;
int count = 0;
printf("Enter frame length: ");
scanf("%d", &n);
printf("Enter input frame (0's and 1's only):\n");
for(i = 0; i < n; i++) {
scanf("%d", &ip_frame[i]);
}
// Bit stuffing logic
for(i = 0; i < n; i++) {
op_frame[j++] = ip_frame[i];
if(ip_frame[i] == 1) {
count++;
if(count == 5) {
op_frame[j++] = 0; // Stuff a 0 after five 1s
count = 0; // Reset count after stuffing
}
} else {
count = 0; // Reset count if a 0 is found
}
}
printf("\nAfter bit stuffing, the frame is:\n");
for(i = 0; i < j; i++) {
printf("%d", op_frame[i]);
}
printf("\n");
}
Output:
$ gcc bit_stuffing.c
$ ./a.out
enter frame length: 9
Enter input frame (0's & 1's only) :
0
1
0
1
1
1
1
1
1
After stuffing, the frame is:
0101111101
Character Stuffing :
Source Code:
File Name: char_stuffing.c
#include<stdio.h>
#include<string.h>
void main()
{
int i = 0, j = 0, n, pos;
char a[20], b[100], ch;
printf("Enter the string:\n");
scanf("%s", a);
n = strlen(a);
printf("Enter position to insert the character:\n");
scanf("%d", &pos);
// Validate position
while(pos < 1 || pos > n + 1)
{
printf("Invalid position, enter again: ");
scanf("%d", &pos);
}
getchar(); // Clear newline left by previous scanf
printf("Enter the character to stuff:\n");
ch = getchar();
// Add starting frame delimiter
b[0] = 'd'; b[1] = 'l'; b[2] = 'e'; b[3] = 's'; b[4] = 't'; b[5] = 'x';
j = 6;
for (i = 0; i < n; i++)
{
// Insert stuffing character at given position
if (i == pos - 1)
{
b[j++] = 'd'; b[j++] = 'l'; b[j++] = 'e';
b[j++] = ch;
b[j++] = 'd'; b[j++] = 'l'; b[j++] = 'e';
}
// Escape 'dle' sequence
if (a[i] == 'd' && a[i + 1] == 'l' && a[i + 2] == 'e')
{
b[j++] = 'd'; b[j++] = 'l'; b[j++] = 'e';
}
b[j++] = a[i];
}
// If position is at the end
if (pos == n + 1)
{
b[j++] = 'd'; b[j++] = 'l'; b[j++] = 'e';
b[j++] = ch;
b[j++] = 'd'; b[j++] = 'l'; b[j++] = 'e';
}
// Add ending frame delimiter
b[j++] = 'd'; b[j++] = 'l'; b[j++] = 'e'; b[j++] = 'e'; b[j++] = 't'; b[j++] = 'x';
b[j] = '\0';
printf("\nFrame after stuffing:\n%s\n", b);
}
Output:
$ gcc char_stuffing.c
$ ./a.out
Enter the string:
madhu
Enter position to insert the character:
2
Enter the character to stuff:
k
Frame after stuffing:
dlestxmdlekdleadhudleetx
Character Count :
Source Code:
File Name: char_count.c
#include<stdio.h>
#include<string.h>
char data[20][20];
int n;
int main() {
int i, j, ch;
char tmp[20][30]; // Stores frames with character count
printf("Enter the number of frames: ");
scanf("%d", &n);
getchar(); // clear newline after scanf
// Input the frames
for (i = 1; i <= n; i++) {
printf("Frame %d: ", i);
fgets(data[i], sizeof(data[i]), stdin);
data[i][strcspn(data[i], "\n")] = '\0'; // remove newline
}
// Creating frames with count prefix
for (i = 1; i <= n; i++) {
int len = strlen(data[i]);
tmp[i][0] = len + '0'; // store count as char (e.g., '5')
tmp[i][1] = '\0';
strcat(tmp[i], data[i]); // append actual data after count
}
// AT THE SENDER
printf("\n\t\tAT THE SENDER:\n");
printf("Data as frames:\n");
for (i = 1; i <= n; i++) {
printf("Frame %d: ", i);
puts(tmp[i]);
}
printf("Data transmitted: ");
for (i = 1; i <= n; i++) {
printf("%s", tmp[i]);
}
// AT THE RECEIVER
printf("\n\n\t\tAT THE RECEIVER:\n");
printf("The data received: ");
for (i = 1; i <= n; i++) {
ch = tmp[i][0] - '0'; // get count
for (j = 0; j < ch; j++) {
data[i][j] = tmp[i][j + 1]; // skip count character
}
data[i][j] = '\0'; // null terminate
}
printf("\nData after removing count characters: ");
for (i = 1; i <= n; i++) {
printf("%s", data[i]);
}
printf("\n\nData in frame form:\n");
for (i = 1; i <= n; i++) {
printf("Frame %d: ", i);
puts(data[i]);
}
return 0;
}
Output:
$ gcc char_count.c
$ ./a.out
Enter the number of frames: 3
Frame 1: Computer
Frame 2: Networks
Frame 3: Lab
AT THE SENDER:
Data as frames:
Frame 1: 8Computer
Frame 2: 8Networks
Frame 3: 3Lab
Data transmitted: 8Computer8Networks3Lab
AT THE RECEIVER:
The data received:
Data after removing count characters: ComputerNetworksLab
Data in frame form:
Frame 1: Computer
Frame 2: Networks
Frame 3: Lab
๐ป Run This C Program
Implement the data link layer framing methods such as character count, character stuffing and bit stuffing.
The selected C program is loaded
automatically into the editor.
Edit the program, provide input through
STDIN if required
and click Run.
The compiler is embedded using OneCompiler.
Your C program runs inside the embedded
C environment; Study Glance does not execute
C code directly on your web server.
Enter the input values of the program in the
STDIN box before clicking Run.