Skip to content

Instantly share code, notes, and snippets.

@saisumit
Created November 1, 2016 13:45
Show Gist options
  • Select an option

  • Save saisumit/1ec00f1ab47e3144c535a1b3158958d8 to your computer and use it in GitHub Desktop.

Select an option

Save saisumit/1ec00f1ab47e3144c535a1b3158958d8 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
// IMPLEMENT BYTE STUFFING AND CHARACTER STUFFING
// Bit STUFFING ADD ONE ZERO AFTER EVERY 5 CONSECTIVE ONES
/*
Byte stuffing can solvethe problem by reserving a third character to mark occurrences of special characters
in the data.Use reserved characters to indicate the start and end of a frame. For instance, use the two-character
sequence DLE STX (Data-Link Escape, Start of TeXt) to signal the beginning of a frame, and thesequence
DLE ETX (End of TeXt) to flag the frame's end.Problem: What happens if the two-character sequence DLE
ETX happens to appear in the frame itself?Solution: Use character stuffing; within the frame, replace
every occurrence of DLE with the two-character sequence DLE DLE. The receiver reverses the processes,
replacing every occurrence of DLE DLE with asingle DLE.Example: If the frame contained ``A B DLE D E DLE'',
the characters transmitted over the channel wouldbe ``DLE STX A B DLE DLE D E DLE DLE DLE ETX
*/
int main ( )
{
string s ;
cin >> s ;
string res = "" ;
int n = s.length ( ) ;
int ctr = 0 ;
for( int i = 0 ; i < n ; i ++ )
{
if ( s[i] == '1' )
{
ctr = 0 ;
while( s[i] == '1' && i < n )
{ ctr ++ ;
res = res + s[i] ;
if( ctr == 5 )
{ res = res + '0' ;
ctr = 0 ;
}
i++;
}
i-- ;
}
else res = res + s[i] ;
}
cout<<"Bit stuffed string"<<endl;
cout<<res<<endl;
string Start = "DLE STX";
string End = "DLE ETX" ;
string add = "DLE";
cin >> s ;
res = Start ;
n = s.length() ;
for( int i = 0 ; i < n ; i ++ )
{
if( i <= n - 3 && s[i] == 'D' && s[i+1] == 'L' && s[i+2] == 'E')
{
res = res + add + add ;
i = i + 2 ;
}
else res = res + s[i] ;
}
res = res + End ;
cout<<"Character stuffed string"<<endl;
cout<<res<<endl ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment