Character Stuffing in C




Theory:

a serious problem occurs when binary data itself will contain flag bytes bit pattern . this situation usually interfere with the framing . one solution is character or byte stuffing where senders data link layer insert a special escape byte just before each "accidental"  flag byte in the data . at the receiving end data link layer remove the escape byte before the data are given to the network layer 

Implementation:

#include<stdio.h>
int main()
{
    char so[100],data[100];
    printf("enter the text: ");
     scanf("%s",so);
    char flag,bit;
    printf("enter the flag: ");
    scanf(" %c",&flag);
    printf("enter the character: ");
    scanf(" %c",&bit);

    //scanf("%c %c",&flag,&bit);

    int size=strlen(so);
    //char data[2*size+3];
     data[0]=flag;
    int i,j;
    for(i=0,j=1;i<size;i++)
    {
        if(so[i]==bit)
        {
            data[j]=so[i];
            j++;
            data[j]='k';
            j++;

        }
        else{
            data[j]=so[i];
            j++;
        }
    }
    data[j]=flag;
    data[j+1]='\0';
    printf("%s",data);
return 0;
}

Output:


Character Stuffing in C Character Stuffing in C Reviewed by Saroar Zahan Sojib on February 22, 2019 Rating: 5

No comments:

ads
Powered by Blogger.