CN-LAB-V-SEM

Exercise 11: Implement Character Count Framing Method to Resolve Framing Errors

Character Count Program in C

#include <stdio.h>
#include <string.h>

int main() {
    char inp[50], out[100];
    int i, pos = 0, f_len, n_frames, d_len, c_len;

    printf("Enter data: ");
    scanf("%s", inp);

    printf("Enter frame size: ");
    scanf("%d", &f_len);

    d_len = strlen(inp);
    n_frames = (d_len + f_len - 1) / f_len;

    for (i = 0; i < n_frames; i++) {
        c_len = (d_len - pos < f_len) ? (d_len - pos) : f_len;
        out[i * (f_len + 1)] = (char)(c_len + '0');
        strncpy(&out[i * (f_len + 1) + 1], &inp[pos], c_len);
        pos += c_len;
    }

    out[n_frames * (f_len + 1)] = '\0';

    printf("Stuffed data is: %s\n", out);

    return 0;
}

Output Example

Enter data: Welcome
Enter frame size: 4
Stuffed data is: 4Wel3come

Explanation

This program demonstrates the Character Count Framing Method used in data communication. The key steps are:

  1. Input and Frame Size: The user provides the data and the desired frame size.
  2. Frame Division: The data is divided into frames of specified size. If the remaining data is smaller than the frame size, it forms the last frame.
  3. Framing Logic:
    • Each frame is prefixed with its size as a character.
    • Frames are concatenated into the output string (out).
  4. Output: The result is a single string where each frame includes its size as a prefix, ensuring that framing errors can be detected or resolved during transmission.

This approach ensures reliable framing in data communication systems.