Write a C program to generate character count.
#include <stdio.h>
int main()
{
long nc = 0;
while (getchar() != EOF)
++nc;
printf("Total characters: %ld\n", nc);
return 0; // Explicit return value for main function
}
write a program to implement data link layer framing methods character
stuffing.
#include <stdio.h>
#include <string.h>
#define FRAME_DELIMITER '~' // Frame delimiter
#define ESCAPE_CHAR '\\' // Escape character for stuffing
// Function to perform character stuffing
void stuff_characters(char *input, char *output) {
int i = 0, j = 0;
while (input[i] != '\0') {
if (input[i] == FRAME_DELIMITER) {
// If the character is the frame delimiter, stuff it
output[j++] = ESCAPE_CHAR;
output[j++] = FRAME_DELIMITER;
} else {
// Otherwise, just copy the character
output[j++] = input[i];
}
i++;
}
output[j] = '\0'; // Null-terminate the output string
}
// Function to frame the data with start and end delimiters
void frame_data(char *stuffed_data, char *framed_data) {
framed_data[0] = FRAME_DELIMITER; // Start frame delimiter
strcpy(framed_data + 1, stuffed_data);
framed_data[strlen(stuffed_data) + 1] = FRAME_DELIMITER; // End frame delimiter
framed_data[strlen(stuffed_data) + 2] = '\0'; // Null-terminate the framed string
}
int main() {
char input[500]; // Buffer to store input data
char stuffed_data[1000]; // Buffer for stuffed data
char framed_data[1000]; // Buffer for framed data
// Step 1: Read input from the user (or a file)
printf("Enter data (type 'end' to finish input):\n");
fgets(input, sizeof(input), stdin);
// Remove the newline character added by fgets
input[strcspn(input, "\n")] = '\0';
// Step 2: Stuff characters in the input data
stuff_characters(input, stuffed_data);
// Step 3: Frame the stuffed data with start and end delimiters
frame_data(stuffed_data, framed_data);
// Step 4: Output the framed and stuffed data
printf("Framed and Stuffed Data: %s\n", framed_data);
return 0;
}
write a program to implement the data link layer framing method bit stuffing.
#include <stdio.h>
#include <string.h>
#define FLAG_SEQUENCE "01111110" // Frame delimiter (7 bits)
// Function to perform bit stuffing
void bit_stuffing(char *data, char *stuffed_data) {
int i = 0, j = 0;
int consecutive_ones = 0; // Count consecutive '1's
while (data[i] != '\0') {
stuffed_data[j++] = data[i];
// Count consecutive '1's
if (data[i] == '1') {
consecutive_ones++;
} else {
consecutive_ones = 0;
}
// If we encounter five consecutive '1's, stuff a '0'
if (consecutive_ones == 5) {
stuffed_data[j++] = '0';
consecutive_ones = 0; // Reset count after stuffing
}
i++;
}
stuffed_data[j] = '\0'; // Null-terminate the stuffed data
}
// Function to frame the data with start and end flag sequences
void frame_data(char *stuffed_data, char *framed_data) {
// Add flag sequence at the start
strcpy(framed_data, FLAG_SEQUENCE);
// Append the stuffed data
strcat(framed_data, stuffed_data);
// Add flag sequence at the end
strcat(framed_data, FLAG_SEQUENCE);
}
int main() {
char input[500]; // Input data (string of bits)
char stuffed_data[1000]; // Stuffed data
char framed_data[1000]; // Framed data
// Step 1: Read the input data (in binary form)
printf("Enter data in binary (e.g., 111110101110): ");
scanf("%s", input);
// Step 2: Perform bit stuffing
bit_stuffing(input, stuffed_data);
// Step 3: Frame the stuffed data with flag sequences
frame_data(stuffed_data, framed_data);
// Step 4: Output the framed and stuffed data
printf("Framed and Stuffed Data: %s\n", framed_data);
return 0;
}