Friday, 12 October 2012

Programme for hamming code


Programme for hamming code

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>

void main()
{

char sender[20],buffer[50],reciver[20];
int d,p=0,i=0,j,k,length,power,count;
clrscr();

printf("\n\n Enter the Message in bit form : ");
gets(sender);

d=strlen(sender);
//power
for(p=0; ;p++)
{
if(pow(2,p)>=d+p)
{
break;
}
}

p--;

/* COPY THE BITS INTO BUFFER */
i=1;
power=0;
j=0;

do
{
if(i==pow(2,power))
{
buffer[i]='0';
power++;
i++;
}
else
{
buffer[i]=sender[j];
j++;
i++;
}
}while(j<=d);
buffer[i]='\0';
//APPLY BIT IN CODE
count=0;
length=d+p;
j=0;
for(j=0;j<=p;j++)
{
k=pow(2,j);

for(i=k+1;i<=length; )
{

if(k==1)
{
if(buffer[i]=='1')
{
count++;
}
i=i+2;
}
else
{
if(buffer[i]=='1')
{
count++;
}
i++;
if(i%k==0)
{
i=i+k;
}
}
}
if(count%2==1)
{
buffer[k]='1';
}
count=0;
}
printf("\n ENCODED Message is :");
i=1;
do
{
printf("%c",buffer[i]);
i++;
}while(buffer[i]!='\0');
getch();
}

No comments:

Post a Comment