answersLogoWhite

0


Best Answer

#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<process.h>

char a[20],b[50],c[50],ch;

int i=0,n;

void sender();

void receiver();

void main()

{

clrscr();

sender();

receiver();

getch();

}

void sender()

{

int j=0,pos;

printf("\n\n Sender side \n\n");

printf("Enter string : ");

scanf("%s",&a);

n=strlen(a);

printf("\n Enter position : ");

scanf("%d",&pos);

label: if(pos>n)

{

printf("\n Invalid position, Enter again : ");

scanf("%d",&pos);

goto label;

}

printf("\n Enter the character : ");

ch=getche();

b[0]='d';

b[1]='l';

b[2]='e';

b[3]='s';

b[4]='t';

b[5]='x';

j=6;

while(i<n)

{

if(i==pos-1)

{

b[j]='d';

b[j+1]='l';

b[j+2]='e';

b[j+3]=ch;

b[j+4]='d';

b[j+5]='l';

b[j+6]='e';

j=j+7;

}

if(a[i]=='d' && a[i+1]=='l' && a[i+2]=='e')

{

b[j]='d';

b[j+1]='l';

b[j+2]='e';

j=j+3;

}

b[j]=a[i];

i++;

j++;

}

b[j]='d';

b[j+1]='l';

b[j+2]='e';

b[j+3]='e';

b[j+4]='t';

b[j+5]='x';

b[j+6]='\0';

printf("\nframe after stuffing : %s" ,b);

}

void receiver()

{

int j=6;

printf("\n\n\n\n Receiver side\n\n");

printf("\nThe data came from sender side is : %s",b);

n=strlen(b);

while(j<n-6)

{

if(b[j]=='d' && b[j+1]=='l' && b[j+2]=='e')

{

if(b[j+3]=='d' && b[j+4]=='l' && b[j+5]=='e')

{

c[i]=b[j+3];

c[i+1]=b[j+4];

c[i+2]=b[j+5];

i = i+3;

j = j+6;

}

else if(b[j+4]=='d' && b[j+5]=='l' && b[j+6]=='e')

{

j = j+7;

}

}

else

{

c[i]=b[j];

i++;

j++;

}

}

printf("\n\nOriginal data : %s",c);

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

// BY: BIBHAKAR JHA

// Objective : to implement character stuffing in c

//PROGRAM CODE :

#include<stdio.h>

#include<conio.h>

void main()

{

int i, j, p=0, q=0;

char ary1[50], ary2 [50]=" ", a[6], b[5], char1;

clrscr();

printf("\n Please Enter bit sequence :\n");

scanf("%s", &ary1);

strcat(ary2, "01111110");

if(strlen(ary1)<5)

{

strcat(ary2, ary1);

}

else

{

for(i=0; i<strlen(ary1)-4; i++)

{

for (j=1; j<i+5; j++)

{

a[p++] = ary1[j];

}

a[p]='\0';

if(strcat(a, "11111") ==0)

{

strcat(ary2, "111110");

i=j-1;

}

else

b[0] = ary1[i];

b[1] ='\0';

strcat(ary2, b);

}

}

for(q=i; q<strlen(ary1); q++)

{

a[p++] =ary1[q];

}

a[p] ='\0';

strcat(ary2,a);

strcat(ary2,"01111110");

printf("\n if string after stutffing: %s", ary2);

getch();

}

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

The following program demonstrates character stuffing. Normally, character stuffing is used when transmitting data across a network where the data contains a payload marked with special codes to denote the start and end of the payload within the data. However, if the payload contains those same markers, the payload could be misinterpreted. Character stuffing ensures that the payload is interpreted correctly, provided the receiver knows how the payload was stuffed and can undo the stuffing accordingly.

Since I have no idea how your network is set up, the program doesn't make use of a network. Instead, it will simply pass the data to a function which will process the data and extract the payload. The payload itself is nothing more than a string. In order to mark the payload within the data, special code sequences are used to mark the start and end of the payload. The payload is therefore "framed" by these markers.

However, in order to demonstrate character stuffing, the payload itself contains one of those markers (the end marker). The program will attempt to send the payload to the function without character stuffing first, which will fail because the first end marker will effectively mark the end of the payload, thus cutting the payload short. The process will continue through the remainder of the data and when it encounters the second end marker, it will deem the entire frame invalid because an end marker must have a corresponding start marker.

The program will then send the same payload, this time with character stuffing. There is nothing complex about the character stuffing itself, we simply insert an extra DLE (data-link escape) code in front of all other DLE codes found in the payload. When the payload is stuffed and framed, the process will correctly identify the original payload and no errors will occur.

Note that the process function is not a complete implementation and hasn't been tested with any data other than the provided payload. However it is not intended to emulate a real-world network end-point, it simply demonstrates how character stuffing works within a single data frame. In the real-world, a payload may be spread over hundreds or thousands of data frames, but the character stuffing function remains the same.

#include<iostream>

#include<string>

// Global C0 control codes

const char STX = 0x02; // start of text

const char ETX = 0x03; // end of text

const char DLE = 0x10; // data-link escape

// Global control sequences marking start and end of payload

const char DLE_STX[3] = {DLE, STX, 0};

const char DLE_ETX[3] = {DLE, ETX, 0};

// The character stuffing function.

void stuff_payload (std::string& payload)

{

// Traverse the payload

for (std::string::iterator it=payload.begin(); it!=payload.end(); ++it)

{

// If the current character is a DLE, insert

// another DLE code immediately before it.

if (*it == DLE)

{

// When we insert, the iterator will refer to the inserted

// DLE code, so we increment so we're referring to the one

// we were originally referring to (now the second DLE).

it = payload.insert (it, DLE) + 1;

}

}

// Display the stuffed payload.

std::cout << "Stuffed payload:\t" << payload << std::endl;

}

// The process function processes a frame that contains a payload.

// Only the payload itself will be printed. The frame must include

// control sequences DLE+STX and DLE+ETX to denote where the payload

// begins and ends within the frame. If the payload itself contains

// control sequences, the payload must be stuffed in order to be

// processed correctly.

void process (const std::string frame, const bool stuffed)

{

// Begin output...

std::cout << "Received payload:\t";

// Status flags.

bool in_payload = false;

bool DLE_flag = false;

bool error = false;

// Begin processing one character at a time.

for (std::string::const_iterator it=frame.begin(); it!=frame.end() && !error; ++it)

{

// Jump to the appropriate code point

switch (*it)

{

case DLE: // data-link escape code

if (!DLE_flag)

{

// The DLE flag is currently off, so turn it on and fetch the next character.

DLE_flag = true;

continue;

}

// The DLE flag is on so this must be the second DLE we've encountered.

// This is only valid inside a stuffed payload.

if (in_payload && stuffed)

{

// Print one DLE, turn the flag off and fetch the next character.

std::cout << *it;

DLE_flag = false;

continue;

}

// If we get this far, we've found two consecutive DLE codes.

// This is invalid both outside of a payload and inside an

// unstuffed payload. Stop processing!

std::cerr << "\nERROR: the frame is invalid!";

error = true;

break;

case (STX): // start of text code

if (!in_payload)

{

if (DLE_flag)

{

// We've found a DLE+STX sequence outside of the payload

// so whatever follows must be part of the payload. Set

// the flags and fetch the next character.

in_payload = true;

DLE_flag = false;

continue;

}

}

// Control seqeuences are only valid inside a payload

// when the payload is stuffed.

else if (stuffed)

{

// Was the previous character a DLE?

if (DLE_flag)

{

// Print it and turn the flag off.

std::cout << DLE;

DLE_flag = false;

}

// Print the control code and fetch the next character.

std::cout << STX;

continue;

}

// If we get this far we've found an STX but it's either inside

// an unstuffed payload, or it is outside a payload but doesn't

// have a DLE control code in front of it. Stop processing!

std::cerr << "\nERROR: the frame is invalid!";

error = true;

break;

case (ETX): // End of text code

// Only ever valid inside a payload.

if (in_payload)

{

// Was the previous character a DLE?

if (DLE_flag)

{

// We've reached the end of the payload. Reset

// the flags and fetch the next character

in_payload = false;

DLE_flag = false;

continue;

}

// ETX without DLE is only valid in stuffed payloads.

if (stuffed)

{

std::cout << ETX;

continue;

}

}

// If we get this far, ETX was either outside of a payload

// or was inside an unstuffed payload. Stop processing!

std::cerr << "\nERROR: invalid input sequence!";

error = true;

break;

default:

// Note: we're ignoring all other control sequences purely

// for the purpose of brevity. In the real-world, each

// significant sequence would be handled be a separate

// case, as per the above.

if (in_payload)

{

// We're in a payload so print the character and

// fetch the next character.

std::cout << *it;

break;

}

// If we get this far we're not in a payload. Stop processing!

std::cerr << "\nERROR: the frame is invalid!";

error = true;

}

}

// Finished processing. End of the line.

std::cout << '\n' << std::endl;

}

// The sender function formats a payload for processing.

// If with_stuffing is true, the payload is stuffed.

// The payload is then framed with start and end sequences

// and passed to the processor for processing.

void send (std::string payload, const bool with_stuffing)

{

// Display the original payload.

std::cout << "Original payload:\t" << payload << std::endl;

// Add stuffing, if required.

if (with_stuffing)

stuff_payload (payload);

// Now pack the payload into a frame ready for sending.

std::string frame (DLE_STX);

frame += payload;

frame += DLE_ETX;

// Display the entire frame that we'll be sending.

std::cout << "Framed payload (sent):\t" << frame << std::endl;

// Transmit...

process (frame, with_stuffing);

}

int main()

{

// Create a payload that includes a control sequence.

std::string payload = "The character sequence ";

payload += DLE_ETX;

payload += " denotes the end of a payload!";

// Send the payload without character stuffing.

send (payload, false);

// Resend the payload with character stuffing.

send (payload, true);

}

Example output:

Original payload: The character sequence ►♥ denotes the end of a payload!

Framed payload (sent): ►☻The character sequence ►♥ denotes the end of a payload!►♥

Received payload: The character sequence

ERROR: the frame is invalid!

Original payload: The character sequence ►♥ denotes the end of a payload!

Stuffed payload: The character sequence ►►♥ denotes the end of a payload!

Framed payload (sent): ►☻The character sequence ►►♥ denotes the end of a payload!►♥

Received payload: The character sequence ►♥ denotes the end of a payload!

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the c program for character stuffing and destuffing?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write a program in c language to implement framing methods like character stuffing?

A program in c language to implement framing methods like character stuffing can be grave sizeCRC-32 and the variable c50.


Byte stuffing program in c?

teri bhen ki.....................


How is memory allocated in c program for a character data type?

One byte for every character.


C program to find the largest and smallest character in a word?

helicopter


Why and operator can not be used in string of c program?

Any character can be used in string, except for \\0. char example [] = "A&amp;B|C";


Write a program to adentify whether a character entered by a user is a vowel or a consonant?

You can check the value of a character by using if statements.Also, note that this code does not check for capital letters./* code */#include int main(){char c;c = getc(stdin);if (c 'o') {/* If the character is a vowel, this code will be executed. */} else if (c >= 'a' && c


What are the benefits of data stuffing?

The following data fragment occurs in the middle of a data stream for which the byte-stuffing algorithm described in the text is used: A B ESC C ESC FLAG FLAG D. What is the output after stuffing?


Write a program to initialize a character and print whether it is uppercase lowercase or special character in java module 1?

I'll just write a function to do that, I hope the good people won't try to run it as it is.... void function() { char c = 'a'; if( c &gt;= 'a' &amp;&amp; c &lt;='z' ) System.out.println("LowerCase"); else if( c&gt;='A' &amp;&amp; c &lt;='Z' ) System.out.println("UpperCase"); else System.out.println("Special Character"); }


How is the getchar function used in a C program?

The getchar() is used in 'C' programming language because it can read the character from the Standard input(i.e..from the user keyboard),and converts in to the ASCII value.


Can you give an example of array using character variable in c program?

the example of array over charcter variables is char ["string"]


Write a C program to find out whether character passes through keybroad is a digit or not?

hey..it s simple..make use of built-in function isdigit(character)..it s in stdlib.h...


Features of c program?

the features of a C program