// bit stuffing

#include<stdio.h>
#include<string.h>
int main()
{
    int a[20],b[30],i,j,k,count,n;
    printf("Enter frame size:");
    scanf("%d",&n);
    printf("Enter the frame in the form of 0 and 1 :");
    for(i=0; i<n; i++)
        scanf("%d",&a[i]);
    i=0;
    count=1;
    j=0;
    while(i<n)
    {
        if(a[i]==1)
        {
            b[j]=a[i];
            for(k=i+1; a[k]==1 && k<n && count<5; k++)
            {
                j++;
                b[j]=a[k];
                count++;
                if(count==5)
                {
                    j++;
                    b[j]=0;
                }
                i=k;
            }
        }
        else
        {
            b[j]=a[i];
        }
        i++;
        j++;
    }
    printf("After Bit Stuffing :");
    for(i=0; i<j; i++)
        printf("%d",b[i]);
    return 0;
}
//0 1 0 1 1 1 1 1 1 0 0 1







// byte stuffing

#include<stdio.h>             
#include<string.h>         
int main(){
        char a[20],b[20];
        int i,n,j;
        char f,s;
        printf("Enter the size of the frame : ");
        scanf("%d",&n);
        n=n*2;
        printf("\nEnter the characters in frame : \n");
        for(i=0;i<n;i++)
                scanf("%c",&a[i]);
        printf("\n FRAME \n ");
        for(i=0;i<n;i++)
                printf("%c",a[i]);
        j=0;
        for(i=0;i<n;i++)
        {
                if(a[i]=='f')
                {
                        b[j]='s';
                        j++;
                        b[j]=a[i];

                }
                else if(a[i]=='s')
                {
                        b[j]='s';
                        j++;
                        b[j]=a[i];
                }
                else
                        b[j]=a[i];

                j++;
        }
        printf("\n RESULT \n");
        printf("f");
        for(i=0;i<j;i++)
        {
                printf("\n");
                printf("%c",b[i]);
        }
        printf("\nf");
}
//8
//11000010







// CRC

#include<bits/stdc++.h>
using namespace std;


string xor1(string a, string b)
{


string result = "";

int n = b.length();


for(int i = 1; i < n; i++)
{
if (a[i] == b[i])
result += "0";
else
result += "1";
}
return result;
}


string mod2div(string divident, string divisor)
{


int pick = divisor.length();



string tmp = divident.substr(0, pick);

int n = divident.length();

while (pick < n)
{
if (tmp[0] == '1')


tmp = xor1(divisor, tmp) + divident[pick];
else


tmp = xor1(std::string(pick, '0'), tmp) +
divident[pick];

pick += 1;
}


if (tmp[0] == '1')
tmp = xor1(divisor, tmp);
else
tmp = xor1(std::string(pick, '0'), tmp);

return tmp;
}


void encodeData(string data, string key)
{
int l_key = key.length();


string appended_data = (data +
std::string(
l_key - 1, '0'));

string remainder = mod2div(appended_data, key);


string codeword = data + remainder;
cout << "Remainder : "
<< remainder << "\n";
cout << "Encoded Data (Data + Remainder) :"
<< codeword << "\n";
}

int main()
{
string data = "100100";
string key = "1101";

encodeData(data, key);

return 0;
}









// error detection

#include <stdio.h>

int main()
{
    int arr[8];
    int arr1[8];
    int arr2[8];
    int arr3[8];
    int carry=0;
    int carry1=0;
    printf("Enter the first array : ");
    for(int i=0;i<8;i++)
    {
        scanf("%d",&arr[i]);
    }
    printf("Enter the Second array : ");
    for(int i=0;i<8;i++)
    {
        scanf("%d",&arr1[i]);
    }
    printf("Performing addition");
    for(int i=7 ; i>=0 ; i--)
    {
        if(arr[i]==0 && arr1[i]==0)
        {
            if(carry==0)
            {
                arr2[i]=0;
                carry=0;  
            }
            else
            {
                arr2[i]=1;
                carry=0;    
            }
        }
        else if((arr[i]==1 && arr1[i]==0) || (arr[i]==0 && arr1[i]==1))
        {
            if(carry==0)
            {
                arr2[i]=1;
                carry=0;  
            }
            else
            {
                arr2[i]=0;
                carry=1;    
            }
        }
        else
        {
            if(carry==0)
            {
                arr2[i]=0;
                carry=1;  
            }
            else
            {
                arr2[i]=1;
                carry=1;    
            }
        }
    }
    if(carry==1)
    {
        for(int i=7;i>=0;i--)
        {
            if(arr2[i]==1)
            {
                if(carry1 = 0)
                {
                    carry1=1;
                    arr2[i]=0;
                }
                else
                {
                    carry1=1;
                    arr2[i]=1;
                }
            }
            else
            {
                carry1=0;
                arr2[i]=1;
                break;
            }
        }
    }
    for(int i=0;i<8;i++)
    {
        if (arr2[i]==1)
        {
            arr3[i]=0;
        }
        else
        {
            arr3[i]=1;
        }
    }
    printf("\n");
    for(int i=0;i<8;i++)
    {
        printf("%d",arr3[i]);
    }
   

    return 0;
}

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: