Aim: Write a program in C, to perform character stuffing.
Software Required: Turbo C Program:
#include <stdio.h>
#include <string.h>
#define FRAME_START_DELIMITER '~'
#define FRAME_END_DELIMITER '~'
// Function to perform character stuffing
void characterStuffing(char* input, char* stuffed) {
int stuffed_index = 0;
int i = 0;
for (i = 0; i < strlen(input); i++) {
if (input[i] == FRAME_START_DELIMITER || input[i] == FRAME_END_DELIMITER) {
// Add frame delimiters before and after the stuffed character
stuffed[stuffed_index++] = FRAME_START_DELIMITER;
stuffed[stuffed_index++] = input[i];
stuffed[stuffed_index++] = FRAME_END_DELIMITER;
} else {
// Just copy the character as it is
stuffed[stuffed_index++] = input[i];
}
// Null terminate the stuffed string
stuffed[stuffed_index] = '\0';
int main() {
char input[100], stuffed[200];
// Get the input string safely
printf("Enter the input string: ");
fgets(input, sizeof(input), stdin);
// Remove the newline character that fgets may add
input[strcspn(input, "\n")] = '\0';
// Perform character stuffing
characterStuffing(input, stuffed);
// Print the results
printf("Original string: %s\n", input);
printf("Stuffed string: %s\n", stuffed);
return 0;
}