Pages - Menu

Tuesday, 22 July 2014

Implementation of bit stuffing in C.

Here is the code..

 #include<stdio.h>
int main()
{
   int a[20],i,j,k;

printf("\nEnter a 10 digit no. for stuffing-- ");
   for(i=0;i<10;i++)
   {
     scanf("%d ",&a[i]);
   }
   k=10;
   for(i=0;i<k;)
   {
      if((a[i]==1) && (a[i+1]==1) && (a[i+2]==1)  && (a[i+3]==1) &&  (a[i+4]==1))
      {
i=i+5;
k=k+1;
for(j=k+1;j>i;j--)
{
  a[j]=a[j-1];
}
a[i]=0;
      }
      else
      {
i=i+1;
      }
   }
   printf("\n Stuffed no. is ");
   for(i=0;i<k;i++)
   {
     printf("%d",a[i]);
   }
return 1;
}


For Turbo C users.

 #include<stdio.h>
#include<conio.h>
void main()
{
   int a[20],i,j,k;
   clrscr();
   printf("\nEnter a 10 digit no. for stuffing-- ");
   for(i=0;i<10;i++)
   {
     scanf("%d ",&a[i]);
   }
   k=10;
   for(i=0;i<k;)
   {
      if((a[i]==1) && (a[i+1]==1) && (a[i+2]==1)  && (a[i+3]==1) &&  (a[i+4]==1))
      {
i=i+5;
k=k+1;
for(j=k+1;j>i;j--)
{
  a[j]=a[j-1];
}
a[i]=0;
      }
      else
      {
i=i+1;
      }
   }
   printf("\n Stuffed no. is ");
   for(i=0;i<k;i++)
   {
     printf("%d",a[i]);
   }
getch();
}

No comments:

Post a Comment