Sie sind auf Seite 1von 26

//Program No 1

//Write a program to find the parity bit (even or odd).

#include<stdio.h>
#include<conio.h>
void main()
{
char c,*s;
int *ss;
int a,i,j,k,n,l;
clrscr();
printf("Enter the string\n");
gets(s);
j=0;
n=strlen(s);
printf("length of entered string is : %d\n",n);
printf("Enter what you want : even parity : '1' or odd parity : '2'\n");
scanf("%d",&k);
printf("The binary values for all the character in the entered string are :\n");
for(l=0;l<n;l++)
{
a=*(s+l);
for(i=0;i<8;i++)
{
(*(ss+i))=a%2;
a=a/2;
}
for(i=7;i>=0;i--)
{
printf("%d",*(ss+i));
}

printf("\n");
}
printf("final data sent (with parity bit in the last) :\n");
for(l=0;l<n;l++)
{
a=*(s+l);
for(i=0;i<8;i++)
{
if(((*(ss+i))=a%2)==1)
j++;
a=a/2;
}
for(i=7;i>=0;i--)
{
printf("%d",*(ss+i));
}
if(k==1)
{
if(j%2==0)
else printf("1");
}
else
{
if(j%2==0)
printf("1");
else printf("0");
}
j=0;
printf("\n");
}
getch();
}

//Program No. 2
//Write a program to implement bit stuffing and destuffing.

#include<stdio.h>
#include<conio.h>
void main()
{
char c,*s,*t;
int *ss;
int a,i,j,k=0,n,l;
clrscr();
printf("Enter the string\n");
gets(s);
j=0;
n=strlen(s);
printf("length of entered string is : %d\n",n);
printf("the entered data is : ");
puts(s);
printf("\n");
t=s+2*n;
for(l=0,a=0;l<n;l++,a++)
{
if(j<5)
{
*(t+a)=*(s+l);
}
else
{
*(t+a)='0';
j=1;
l--;
k=1;

}
if(*(s+l)=='1'&&k==0)
j++;
else
j=0;
k=0;
}
*(t+a)='\0';
printf("\n");
n=strlen(t);
printf("length of transmitted data is : %d\n",n);
printf("The stuffed data is : %s\n",t);
k=0;
j=0;
for(l=0,a=0;l<n;l++,a++)
{
if(j<5)
{
*(s+a)=*(t+l);
}
else
{
j=0;
l++;
k=1;
}
if(*(s+l)=='1'&&k==0)
j++;
else
j=0;
k=0;
}

*(s+a)='\0';
printf("The destuffed data is : %s",s);
getch();
}

//Program No. 3
//Write a program to implement character stuffing and destuffing.
#include<stdio.h>
#include<conio.h>
void main()
{
char c,*s,*d, *r;
int *ss;
int a=0,i=0,j=0,k=0,n=0,l=0;
clrscr();
printf("Enter the string\n");
gets(s);
j=0;
n=strlen(s);
printf("\n");
*(d + i++)='F';
while((c=*(s+j))!='\0')
{
if(c=='$' || c=='F' || c=='f')
{
*(d+i)='$';
i++;
}
*(d+i)=*(s+j);
j++;
i++;
}
*(d+i)='F';
*(d+i+1)='\0';
printf("The stuffed data is : ");
puts(d);
j=1;

i=0;
while((c=*(d+j))!='\0')
{
if(c=='$' && l==1)
{
j++;
l=0;
}
else
{
*(r+i)=*(d+j);
i++;
j++;
l=1;
}
}
*(r+i-1)='\0';
printf("The destuffed data is : %s",r);
getch();

//Program No. 4
//Write a program to implement CRC generator and checker.
#include<stdio.h>
#include<conio.h>
void main()
{
char c,*ss,*t;
char *ds,*dv,*td,*rm,*q;
int nds=0,ndv=0,a,i,j,k=0,n,l;
clrscr();
printf("Enter the divisor\n");
gets(ds);
printf("length of divisor = %d\n",nds=strlen(ds));
printf("Enter the dividend\n");
gets(dv);
printf("length of dividend = %d\n",ndv=strlen(dv));
for(i=0;i<nds-1;i++)
*(dv+ndv+i)='0';
*(dv+ndv+i)='\0';
printf("dividend after appending zeros is ");
puts(dv);
for(j=0;j<nds;j++)
*(rm+j)=*(dv+j);
*(rm+j)='\0';
for(i=0;i<ndv;i++)
{
if(i==0 ? 1 : *(rm)=='1')
{
for(j=0;j<nds-1;j++)
{
*(rm+j)=(*(ds+j+1) == *(rm+j+1) ? '0': '1' );
}

*(rm+j)=*(dv+j+i+1);
*(rm+nds)='\0';
*(q+i)='1';
}
else
{
for(j=0;j<nds-1;j++)
*(rm+j)=*(rm+j+1);
*(rm+j)=*(dv+j+i+1);
*(q+i)='0';
}
}
*(q+i)='\0';
printf("\ncrc is ");
puts(rm);
printf("quotient is %s\n\n",q);
for(i=0;i<ndv;i++)
*(td+i)=*(dv+i);
for(j=0;j<nds-1;j++)
*(td+i+j)=*(rm+j);
*(td+i+j)='\0';
printf("The transmitted data is ");
puts(td);
//at receiving side
for(j=0;j<nds;j++)
*(rm+j)=*(td+j);
*(rm+j)='\0';
for(i=0;i<ndv;i++)
{
if(i==0 ? 1 : *(rm)=='1')
{
for(j=0;j<nds-1;j++)

{
*(rm+j)=(*(ds+j+1) == *(rm+j+1) ? '0': '1' );
}
*(rm+j)=*(td+j+i+1);
*(rm+nds)='\0';
}
else
{
for(j=0;j<nds-1;j++)
*(rm+j)=*(rm+j+1);
*(rm+j)=*(td+j+i+1);
}
}
printf("\nremainder at receiving side is ");
puts(rm);
printf("\n");
for(i=0;i<nds-1;i++)
if(*(rm+i)=='1')
{
printf("The remainder at receiving side is not zero, hence there is an error");
break;
}
if(i==nds-1)
printf("As remainder is zero, there is not any error");
getch();
}

//Program No. 5
//Write a program to implement Hamming Code generator and checker.
#include<stdio.h>
#include<math.h>
void main()
{
int i=0,j=0,a[12],b[12],l=0;
clrscr();
printf("Enter the data bits one-by-one\n");
scanf("%d",&a[11]);
scanf("%d",&a[10]);
scanf("%d",&a[9]);
scanf("%d",&a[7]);
scanf("%d",&a[6]);
scanf("%d",&a[5]);
scanf("%d",&a[3]);
a[1]=a[3]^a[5]^a[7]^a[9]^a[11];
a[2]=a[3]^a[6]^a[7]^a[10]^a[11];
a[4]=a[5]^a[6]^a[7];
a[8]=a[9]^a[10]^a[11];
printf("The sent data is ");
for(i=11;i>0;i--)
printf("%d",a[i]);
printf("\nchecking the received data\n");
b[1]=a[1]^a[3]^a[5]^a[7]^a[9]^a[11];
b[2]=a[2]^a[3]^a[6]^a[7]^a[10]^a[11];
b[4]=a[4]^a[5]^a[6]^a[7];
b[8]=a[8]^a[9]^a[10]^a[11];
printf("p1=%d, p2=%d, p4=%d, p8=%d",b[1],b[2],b[4],b[8]);
printf("\nsince all parity bits are zero, hence the data is received correctly");
printf("\nNow doing error in the received data, please enter the bit no <=11 : ");
scanf("%d",&i);

if(a[i]==0)
a[i]=1;
else
a[i]=0;
for(i=11;i>0;i--)
printf("%d",a[i]);
printf("\nagain checking the received data\n");
b[1]=a[1]^a[3]^a[5]^a[7]^a[9]^a[11];
b[2]=a[2]^a[3]^a[6]^a[7]^a[10]^a[11];
b[4]=a[4]^a[5]^a[6]^a[7];
b[8]=a[8]^a[9]^a[10]^a[11];
printf("p1=%d, p2=%d, p4=%d, p8=%d",b[1],b[2],b[4],b[8]);
printf("\nsince all parity bits are not zero, hence there is an error in bit : ");
l=b[1]+b[2]*pow(2,1)+b[4]*pow(2,2)+b[8]*pow(2,3);
printf("%d",l);
printf("\ncorrecting the data : ");
if(a[l]==1)
a[l]=0;
else
a[l]=1;
for(i=11;i>0;i--)
printf("%d",a[i]);
//Program No. 6
//Write a program to implement Caesar Cipher method.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
char *p;
clrscr();
printf("Enter the character string\n");

gets(p);
i=strlen(p);
for(j=0;j<i;j++)
{
if((*(p+j)>87 && *(p+j)<91) || (*(p+j)>119 && *(p+j)<123))
*(p+j)=*(p+j)+3-26;
else
*(p+j)=*(p+j)+3;
}
printf("The encrypted string is: ");
puts(p);
printf("Again the decrypted string is : ");
for(j=0;j<i;j++)
{
if((*(p+j)>64 && *(p+j)<68) || (*(p+j)>96 && *(p+j)<100))
*(p+j)=*(p+j)-3+26;
else
*(p+j)=*(p+j)-3;
}
puts(p);
getch();
}

//Program No. 7
//Write a program to implement RSA.
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int i=0,j=0,d=0,e=0,N=0,p=0,q=0,fn=0;
long C=0;
long M=0;
clrscr();
printf("Enter the prime number p : ");
scanf("%d",&p);
printf("Enter the prime number q : ");
scanf("%d",&q);
printf("N is %d\n",N=p*q);
printf("fn is %d\n",fn=(p-1)*(q-1));
j=2;
while(1)
{
e=j;
if(fn%e != 0 )
{
break;
}
j++;
}
printf("The value of e is %d\n",e);
while(1)
{
d=i;
if((d*e)%fn==1)

{
break;
}
i++;
}
printf("The value of d is %d\n",d);
printf("Enter the value of M : ");
scanf("%ld",&M);
C=pow(M,e);
C=C%N;
M=pow(C,d);
M=M%N;
printf("The encrypted message is : %ld\n",C);
printf("The decrypted message is : %ld",M);
getch();
}

//Program No. 8
//Write a program to implement Huffman encoding and decoding.
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct tree{
int value;
char ch;
struct tree *left;
struct tree *right;
struct tree *parent;
} *root,temp1,temp2,*node1,*node2,*start;
char *code[10],codechar[10];
int c1=0;
void traverse(struct tree * ss,char c,int i,int j)
{
int k=0;
if(ss->left==NULL && ss->right==NULL)
{
*(code[i]+j)='\0';
node1=ss->parent;
node2=ss;
codechar[i]=ss->ch;
while(--j >= 0)
{
if(node2 == node1->left)
*(code[i]+j)='0';
else
*(code[i]+j)='1';
node2=node1;
node1=node1->parent;
}

}
else
{
j++;
traverse(ss->left,'l',c1,j);
traverse(ss->right,'r',++c1,j);
}
}
void main()
{
int i=0,tempv1=0,sum=0,flag=0,tempv2=0,j=0,k=0,n=0,m=0,chf[10];//chf=character
frequency
char *p[10],chs[10],*q,*text,c,*textcode;//chs - characters
clrscr();
printf("Enter the no. of characters used : ");
scanf("%d",&n);
gets(q); getch();
printf("Enter the characters and their respective frequencies\n");
for(i=0;i<n;i++)
{
code[i]=(char *)malloc(10);
printf("Enter the character %d : ",i+1);
scanf("%c",&chs[i]);
gets(q); getch();
printf("Enter frequency of character %d : ",i+1);
scanf("%d",&chf[i]);
gets(q); getch();
sum+=chf[i];
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)

{
if(chf[j]>chf[j+1])
{
(chf[j]=chf[j]+chf[j+1]-(chf[j+1]=chf[j]));
(chs[j]=chs[j]+chs[j+1]-(chs[j+1]=chs[j]));
}
}
}
k=0;
flag=0;
for(i=0;i<n-1;i++,k++)
{
if(flag==1)
{
root=(struct tree *)malloc(sizeof(struct tree));
node1=(struct tree *)malloc(sizeof(struct tree));
node1->value=chf[k];
node1->ch=chs[k];
node1->left=NULL;
node1->right=NULL;
node1->parent=root;
root->left=node1;
root->right=start;
start->parent=root;
root->value = node1->value + start->value;
start=root;
flag=0;
}
else
{
root=(struct tree *)malloc(sizeof(struct tree));
node1=(struct tree *)malloc(sizeof(struct tree));

node2=(struct tree *)malloc(sizeof(struct tree));


node1->value=chf[k];
node1->ch=chs[k];
node1->left=NULL;
node1->right=NULL;
node1->parent=root;
node2->value=chf[k+1];
node2->ch=chs[k+1];
node2->left=NULL;
node2->right=NULL;
node2->parent=root;
root->left=node1;
root->right=node2;
root->value = node1->value + node2->value;
if(start!=NULL)
{
node1=(struct tree *)malloc(sizeof(struct tree));
tempv1=start->value + root->value;
node1->left=start;
node1->right=root;
node1->value=tempv1;
start->parent=node1;
root->parent=node1;
start=node1;
}
else start=root;
flag=1;
i++;k++;
}
}
node1=start;
k=0;

traverse(start,'r',0,0);
printf("Character codes are : \n");
for(i=0;i<n;i++)
{
printf("%c : %s\n",codechar[i],code[i]);
}
printf("Enter the text to be encoded : ");
gets(text);
getch();
printf("The encoded text is : ");
textcode=(char *)malloc(100);
*textcode='\0';
for(i=0;i<strlen(text);i++)
{
for(j=0;j<n;j++)
{
c=codechar[j];
if(*(text+i) == c)
break;
}
printf("%s",code[j]);
strncat(textcode,code[j],strlen(code[j]));
}
printf("\nThe decoded data is : ");
node1=start;
for(i=0;i<strlen(textcode);i++)
{
if(*(textcode+i) == '0' && node1->left != NULL && node1->right != NULL)
{
node1=node1->left;
}
else if (node1->left != NULL && node1->right != NULL)

{
node1=node1->right;
}
if(node1->left == NULL && node1->right == NULL)
{
printf("%c",node1->ch);
node1=start;
}
}
getch();
}

//Program No. 9
//Write a program to implement LZW compression method.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define concat(a,b) a##b
struct dict
{
char str[10];
int code;
}structure[100];
int insize=0,n=0,ncm=0;//ncm-number of character matched
char *string,*chars;//string-for input string, chars-for input characters
char *inputs[100];//for input symbols being processed
int *codes,outcodes[100];//codes-for codes of input characters, outcodes-holding output codes
int findcode(char ch)
{
int i;
for(i=0;i<n;i++)
{
if(ch==*(chars+i))
{
return *(codes+i);
}
}
return 0;
}
int code(char *p)
{
int i;
if(insize>0)
{

for(i=0;i<insize;i++)
{
if(strcmp(structure[i].str,p) == 0)
{
ncm=strlen(structure[i].str);
return structure[i].code;
}
}
return 0;
}
else
return 0;
}
void main()
{
int i=0,j=0,k=0,l=0,m=0,x=256;
char *p,*q,ch,ch1,temps[10],*temps1,*temps2;
clrscr();
printf("Enter the number of characters used : ");
scanf("%d",&n);
gets(q);
getch();
chars=(char *)malloc(n+1);
*(chars+n)='\0';
codes=(int *)malloc(sizeof(int)*(n+1));
printf("Enter the input characters and their respective codes :\n");
for(i=0;i<n;i++)
{
printf("Enter the character %d : ",i+1);
scanf("%c",(chars+i));
gets(q);
getch();

printf("Enter the code for char %d : ",i+1);


scanf("%d",(codes+i));
gets(q);
getch();
}
printf("Enter the string : ");
gets(string);
getch();
i=0;
ch=*(string+i);
k=0;
while((ch=*(string+i))!='\0')
{
ch=*(string+i);
inputs[insize]=(char *)malloc(10);
j=0;
k=0;
l=0;
temps[k++]=*(string+i+k);
temps[k++]=*(string+i+k);
temps[k]='\0';
j=code(temps);
l=j;
while(j!=0 && *(string+i+k)!='\0')
{
temps[k++]=*(string+i+k);
temps[k]='\0';
j=code(temps);
if(j!=0)
l=j;
}
if(l==0)

{
outcodes[insize]=findcode(ch);
if(*(string+i+1)!='\0')
{
strcpy(structure[insize].str,temps);
structure[insize].code=x++;
}
temps[0]=*(string+i);
temps[1]='\0';
strcpy(inputs[insize],temps);
insize++;
}
else
{
outcodes[insize]=l;
if(*(string+i+ncm)!='\0')
{
strcpy(structure[insize].str,temps);
structure[insize].code=x++;
if((i+k) <= strlen(string))
temps[strlen(temps)-1]='\0';
}
strcpy(inputs[insize],temps);
i=i+strlen(temps)-1;
insize++;
ncm=0;
}
if(j!=0)
{
}
if(strlen(inputs[i])==1)
{

}
i++;
}
printf("The OUTPUT data is :\n");
printf("I/P Symbol\tO/P Code\tAddition to Dictionary\n");
for(i=0;i<insize;i++)
{
if(structure[i].code != 0)
printf("%s\t\t%d\t\t%s\t%d\n",inputs[i],outcodes[i],structure[i].str,structure[i].code);
else
printf("%s\t\t%d\t\t%s\n",inputs[i],outcodes[i],structure[i].str);
}
getch();
}

Das könnte Ihnen auch gefallen