// Source Side
#include<stdio.h>
#include<math.h>
#include<string.h>
int power(int no);
int cnt_ones(int position, int ones);
char ch;
char string[15];
int cnt=1,a=0,i,pos,j,ones=0;
int main(void)
{
FILE *f1, *f2;
// To enter data
f1=fopen("Hammingdata.txt","w");
printf("Enter data (only 7 bits): ");
while((ch=getchar())!=EOF)
{
putc(ch,f1);
}
fclose(f1);
f1=fopen("Hammingdata.txt","r");
fseek(f1,0,2);
printf( "\n\n - - - Reverse print - - - ");
// Read one character and check whether the position is equal to the power of 2, if so fill the space with letter 'r' else with the character read
while(!fseek(f1,-2L,1))
{
ch=getc(f1);
back:
pos=power(a);
if(pos==cnt)
{
string[cnt]='r';
cnt++;
a++;
goto back;
}
else
{
string[cnt]=ch;
cnt++;
}
}
// To print the string in the reverse mode
printf("\n Main string : \n");
for(i=11;i>=1;i--)
{
printf("%c ",string[i]);
}
// Main logic of adding parity bits
for(i=0;i<4;i++)
{
pos=power(i);
if (pos==1)
{
for(j=3;j<=11;j++)
{
if(j==3 || j==5 || j==7 || j==9 || j==11)
{
if(string[j]=='1')
{
ones++;
}
}
if(j==11)
{
cnt_ones(pos,ones);
ones=0;
}
}
}
else if (pos==2)
{
for(j=3;j<=11;j++)
{
if(j==3 || j==6 || j==7 || j==10 || j==11)
{
if(string[j]=='1')
{
ones++;
}
}
if(j==11)
{
cnt_ones(pos,ones);
ones=0;
}
}
}
else if (pos==4)
{
for(j=5;j<=11;j++)
{
if(j==5 || j==6 || j==7)
{
if(string[j]=='1')
{
ones++;
}
}
if(j==11)
{
cnt_ones(pos,ones);
ones=0;
}
}
}
else if (pos==8)
{
for(j=8;j<=11;j++)
{
if(j==8 || j==9 || j==10 || j==11)
{
if(string[j]=='1')
{
ones++;
}
}
if(j==11)
{
cnt_ones(pos,ones);
ones=0;
}
}
}
}
fclose(f1);
f2=fopen("HammingMediator.txt","w");
// To print the string after adding parity bits
printf("\n After parity bits : \n");
for(i=11;i>=1;i--)
{
putc(string[i],f2);
printf("%c ",string[i]);
}
fclose(f2);
return 0;
}
// To calculate the power of a no
int power(int no)
{
int ans=1;
int i;
for (i=1;i<=no;i++)
{
ans=ans*2;
}
return ans;
}
int cnt_ones(int position, int ones)
{
if(ones % 2==1)
{
string[position]='1';
}
else
{
string[position]='0';
}
}
----------------------------------------------------------------------------------------------------------------------------
// Destination Side
#include<stdio.h>
int power(int no);
int cnt_ones(int position, int ones);
char ch,string[15];
int bit[5],cnt=1,a=0,i,pos,j,ones=0,sum=0;
int main()
{
FILE *f1;
f1=fopen("HammingMediator.txt","r");
// Original data from mediator file
printf("\n Original Data \n");
cnt=11;
while((ch=getc(f1))!=EOF)
{
putchar(ch);
string[cnt]=ch;
cnt--;
}
// Data to be printed on the screen
printf(" \n\n Data of the string ---- \n ");
for(i=1;i<=11;i++)
{
printf("%c ",string[i]);
}
fclose(f1);
for(i=0;i<4;i++)
{
pos=power(i);
if (pos==1)
{
for(j=1;j<=11;j++)
{
if(j==1 || j==3 || j==5 || j==7 || j==9 || j==11)
{
if(string[j]=='1')
{
ones++;
}
}
if(j==11)
{
cnt_ones(1,ones);
ones=0;
}
}
}
else if (pos==2)
{
for(j=2;j<=11;j++)
{
if(j==2 || j==3 || j==6 || j==7 || j==10 || j==11)
{
if(string[j]=='1')
{
ones++;
}
}
if(j==11)
{
cnt_ones(2,ones);
ones=0;
}
}
}
else if (pos==4)
{
for(j=4;j<=11;j++)
{
if(j==4 || j==5 || j==6 || j==7)
{
if(string[j]=='1')
{
ones++;
}
}
if(j==11)
{
cnt_ones(3,ones);
ones=0;
}
}
}
else if (pos==8)
{
for(j=8;j<=11;j++)
{
if(j==8 || j==9 || j==10 || j==11)
{
if(string[j]=='1')
{
ones++;
}
}
if(j==11)
{
cnt_ones(4,ones);
ones=0;
}
}
}
}
printf("\n Bit array :\n ");
for(i=1;i<=4;i++)
{
printf("%d ",bit[i]);
}
// To display the position of the modified bit
for(i=1;i<=4;i++)
{
sum=sum+(power(i-1)*bit[i]);
}
printf("\n Error is at %d position \n",sum);
return 0;
}
// To calculate the power of a no
int power(int no)
{
int ans=1;
int i;
for (i=1;i<=no;i++)
{
ans=ans*2;
}
return ans;
}
int cnt_ones(int position, int ones)
{
if(ones % 2==1)
{
bit[position]=1;
}
else
{
bit[position]=0;
}
}
Comments
Post a Comment