Showing posts with label Networking Bit Stuffing in C. Show all posts
Showing posts with label Networking Bit Stuffing in C. Show all posts

Monday, January 21, 2019

Bit Stuffing program in C

 

#include<stdio.h>
int main()
{
    int n;
    printf("Enter the size of buffer\n");
    scanf("%d",&n);
    int arr[n];
    int i;
    printf("Enter the input stream in form of 0 and 1 \n");
    for(i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    int sec[n],count=0,j=0;
    for(i=0;i<n;i++)
    {
        if(arr[i]==1)
        {
            sec[j++]=arr[i];
            count++;
            if(count==5)
            {
                sec[j++]=0;
                count=0;
            }
        }
        else
        {
            sec[j++]=arr[i];
            count=0;
        }

    }
    printf("Input stream is -- \n");
    for(i=0;i<n;i++)
    {
        printf("%d",arr[i]);
    }
    printf("Output stream is -- \n");
    for(i=0;i<j;i++)
    {
        printf("%d",sec[i]);
    }

    return 0;
}