Thursday, 7 April 2016

Character Stuffing

 // PROGRAM FOR CHARACTER STUFFING  
 #include<stdio.h>  
 #include<conio.h>  
 #include<string.h>  
 void main()  
 {  
 int i,j=1;  
 char str1[100], str2[100], str3[100];  
 //clrscr();  
 printf("Enter a string: ");  
 scanf("%s",&str1);  
 //GENERATING ENCODED MESSAGE  
 str2[0] = '@';  
 for(i=0; i< strlen(str1); i++)  
 {  
 if( str1[i]=='@' || str1[i] == '#')  
 {  
 str2[j] = '#'; j++;  
 }  
 str2[j] = str1[i]; j++;  
 }  
 str2[j] = '@'; j++;  
 str2[j] = '\0';  
 printf("Encoded message is: %s", str2);  
 // DECODED MESSAGE or ACTUAL MESSAGE  
 j=0;  
 for(i=1; i<(strlen(str2)-1);i++)  
 {  
 if(str2[i]=='#')  
 i++;  
 str3[j]=str2[i];  
 j++;  
 }  
 str3[j] ='\0';  
 printf("\nDecoded or actual message is: %s", str3);  
 getch();  
 } 

Bit Stuffing

  //PROGRAM FOR BIT-STUFFING   
 #include<stdio.h>  
 #include <string.h>  
 int main()  
 {  
   char ed[100]; //ed - Entered Data  
   char ta[100]; //ta - Temporary Array  
   char dd[100]; //dd - Decoded Data  
   char ecd[100]={'0','1','1','1','1','1','1','0'}; //ecd - EnCoded Data  
   int n, i, j=0, count=0;  
   printf("Enter the string:");  
   scanf("%s",&ed);  
   printf("\nEntered bit pattern: %s",ed);  
 //ENCODING  
   for(i=0; i<strlen(ed); i++)  
   {  
     if(ed[i]=='1')  
       count++;  
     else  
       count = 0;  
     ta[j]=ed[i]; j++;  
     if(count==5)  
     {  
       ta[j] = '0';  
       j++;  
       count = 0;  
     }  
   }  
   ta[j] = '\0';  
   strcat(ta,ecd);  
   strcat(ecd,ta);  
   printf("\nEncoded Data: %s",ecd);  
 //DECODING  
   j=0; count =0;  
   for(i=8; i<strlen(ecd)-8; i++)  
   {  
     if(ecd[i]=='1')  
       count ++;  
     else  
       count = 0;  
     dd[j] = ecd[i]; j++;  
     if(count==5)  
     {  
       i++;  
       count=0;  
     }  
   }  
   dd[j] ='\0';  
   printf("\nDecoded Data: %s", dd);  
   return 0;  
 }  

No comments:

Post a Comment