CN Lab prog-BH

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

1)C program to perform bit stuffing and destuffing on an input data stream

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100

void main()
{
char *p,*q;
char temp;
char in[MAXSIZE];
char stuff[MAXSIZE];
char destuff[MAXSIZE];

int count=0;

printf("enter the input character string (0‘s & 1‘s only):\n");


scanf("%s",in);

p=in;
q=stuff;

while(*p!='\0')
{
if(*p=='0')
{
*q=*p;
q++;
p++;
}
else
{
while(*p=='1' && count!=5)
{
count++;
*q=*p;
q++;
p++;
}

if(count==5)
{
*q='0';
q++;
}
count=0;
}
}
*q='\0';
printf("\nthe stuffed character string is");
printf("\n%s",stuff);

p=stuff;
q=destuff;
while(*p!='\0')
{
if(*p=='0')
{
*q=*p;
q++;
p++;
}
else
{
while(*p=='1' && count!=5)
{
count++;
*q=*p;
q++;
p++;
}
if(count==5)
{
p++;
}
count=0;
}
}
*q='\0';
printf("\nthe destuffed character string is");
printf("\n%s\n",destuff);
getch();
}

2) C program to perform character stuffing and destuffing on an input data stream

#include<stdio.h>
#include<conio.h>
void ins(char,int);
void del(int,int);
char fr[50];
main()
{
int i,ch;
clrscr();
printf("Enter the frame:\t");
gets(fr);
/*Character Stuffing*/
ins('s',0);
ins('e',5);
printf("\n\nStuffed frame is : %s",fr);
getch();
/*Destuffing*/
del(0,6);
del(strlen(fr)-6,6);
printf("\n\nDestuffed bit is \t%s",fr);getch();
return 0;
}
void ins(char in,int p)
{
char dup[50];
int i;
strcpy(dup,fr);
if(in=='s')
{
fr[p]='s';
fr[p+1]='t';
fr[p+2]='x';
fr[p+3]='d';
fr[p+4]='l';
fr[p+5]='e';
for(i=p+6;i<strlen(fr)+6;i++)
{
fr[i]=dup[i-6];
}
}
if(in=='e')
{
strcat(fr,"etxdle");return;
}
}
void del(int q,int n)
{
int i;
for(i=q;i<strlen(fr);i++)
{
fr[i]=fr[i+n];
}
}

3) C Program to find Minimum Distance of Vertices from a given Source in a Graph using Dijkstra’s
Algorithm.
#include<stdio.h>
#include<conio.h>
#define INFINITY 9999
#define MAX 10

void dijkstra(int G[MAX][MAX],int n,int startnode);

void main()
{
int G[MAX][MAX],i,j,n,u;
clrscr();
printf("Enter no. of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");

for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);

printf("\nEnter the starting node:");


scanf("%d",&u);
dijkstra(G,n,u);

getch();
}

void dijkstra(int G[MAX][MAX],int n,int startnode)


{

int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;

//pred[] stores the predecessor of each node


//count gives the number of nodes seen so far
//create the cost matrix
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(G[i][j]==0)
cost[i][j]=INFINITY;
else
cost[i][j]=G[i][j];

//initialize pred[],distance[] and visited[]


for(i=0;i<n;i++)
{
distance[i]=cost[startnode][i];
pred[i]=startnode;
visited[i]=0;
}

distance[startnode]=0;
visited[startnode]=1;
count=1;

while(count<n-1)
{
mindistance=INFINITY;

//nextnode gives the node at minimum distance


for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i])
{
mindistance=distance[i];
nextnode=i;
}

//check if a better path exists through nextnode


visited[nextnode]=1;
for(i=0;i<n;i++)
if(!visited[i])
if(mindistance+cost[nextnode][i]<distance[i])
{
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
count++;
}

//print the path and distance of each node


for(i=0;i<n;i++)
if(i!=startnode)
{
printf("\nDistance of node%d=%d",i,distance[i]);
printf("\nPath=%d",i);

j=i;
do
{
j=pred[j];
printf("<-%d",j);
}while(j!=startnode);
}
}
4)C program to implement Distance Vector Routing using Bellman Ford
Algorithm.
#include<stdio.h>
struct node
{
int dist[20];
int from[20];
}rt[10];
void main()
{
int costmat[20][20];
int nodes,i,j,k,count=0;
printf("\nEnter the number of nodes : ");
scanf("%d",&nodes);//Enter the nodes
printf("\nEnter the cost matrix :\n");
for(i=0;i<nodes;i++)
{
for(j=0;j<nodes;j++)
{
scanf("%d",&costmat[i][j]);
costmat[i][i]=0;
rt[i].dist[j]=costmat[i][j];//initialise the distance equal to cost matrix
rt[i].from[j]=j;
}
}
do
{
count=0;
for(i=0;i<nodes;i++)//We choose arbitary vertex k and we calculate the direct
distance from the node i to k using the cost matrix
//and add the distance from k to node j
for(j=0;j<nodes;j++)
for(k=0;k<nodes;k++)
if(rt[i].dist[j]>costmat[i][k]+rt[k].dist[j])
{//We calculate the minimum distance
rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];
rt[i].from[j]=k;
count++;
}
}while(count!=0);
for(i=0;i<nodes;i++)
{
printf("\n\n For router %d\n",i+1);
for(j=0;j<nodes;j++)
{
printf("\t\nDestination %d Next Hop %d Distance %d
",j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
}
printf("\n\n");
getch();
}

o/p:
/*
A sample run of the program works as:-
Enter the number of nodes :
3
Enter the cost matrix :
0 2 7
2 0 1
7 1 0
For router 1
Destination 1 Next Hop 1 Distance 0
Destination 2 Next Hop 2 Distance 2
Destination 3 Next Hop 3 Distance 3
For router 2
Destination 1 Next Hop 1 Distance 2
Destination 2 Next Hop 2 Distance 0
Destination 3 Next Hop 3 Distance 1
For router 3
Destination 1 Next Hop 1 Distance 3
Destination 2 Next Hop 2 Distance 1
Destination 3 Next Hop 3 Distance 0
*/
5) write C program to implement BROADCAST ROUTING
#include<stdio.h>
struct ed
{
int v1,v2,w;
}
edj[20],temp;
void main()
{
int i,j,n=0,s,d,par[20],s1,d1;
clrscr();
printf("Enter no of edges");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n enter the node1,node2,weight");
scanf("%d%d%d",&edj[i].v1,&edj[i].v2,&edj[i].w);
par[i]=0;
}
clrscr();
printf("Given Graph is:\n");
for(i=0;i<n;i++)
printf("node %d to node %d wieght %d\n",edj[i].v1,edj[i].v2,edj[i].w);
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
if(edj[j].w>edj[i].w)
{
temp=edj[i];
edj[i]=edj[j];
edj[j]=temp;
}
}
printf("\n BROADCAST ROUTING FOR THE GIVEN GRAPH\n");
printf("Node\tNode\tWeight\n");
for(i=0;i<n;i++)
{
s=edj[i].v1;
d=edj[i].v2;
s1=s;
d1=d;
while(par[s1]>0)
s1=par[s1];
while(par[d1]>0)
d1=par[d1];
if(s1!=d1)
{
par[d]=s;
printf("%d\t%d\t%d\n",s,d,edj[i].w);
}
}
getch();
}

/*o/p:

enter node1,node2,weight 1 2 3

enter node1,node2,weight 2 3 2

enter node1,node2,weight 2 4 1

enter node1,node2,weight 3 5 4

enter node1,node2,weight 4 5 5

BROADCAST ROUTING FOR THE GIVEN GRAPH

Node Node Weight

2 4 1

2 3 2

1 2 3

3 5 4

***********************************************/

6)C program to detect error using cyclic redundancy check method.


#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
int i,j,keylen,msglen;
char input[100], key[30],temp[30],quot[100],rem[30],key1[30];
clrscr();
printf("Enter Data: ");
gets(input);
printf("Enter Key: ");
gets(key);
keylen=strlen(key);
msglen=strlen(input);
strcpy(key1,key);
for (i=0;i<keylen-1;i++)
{
input[msglen+i]='0';
}
for (i=0;i<keylen;i++)
temp[i]=input[i];
for (i=0;i<msglen;i++)
{
quot[i]=temp[0];
if(quot[i]=='0')
for (j=0;j<keylen;j++)
key[j]='0'; else
for (j=0;j<keylen;j++)
key[j]=key1[j];
for (j=keylen-1;j>0;j--)
{
if(temp[j]==key[j])
rem[j-1]='0'; else
rem[j-1]='1';
}
rem[keylen-1]=input[i+keylen];
strcpy(temp,rem);
}
strcpy(rem,temp);
printf("\nQuotient is ");
for (i=0;i<msglen;i++)
printf("%c",quot[i]);

printf("\nRemainder is ");
for (i=0;i<keylen-1;i++)
printf("%c",rem[i]);

printf("\nFinal data is: ");


for (i=0;i<msglen;i++)
printf("%c",input[i]);
for (i=0;i<keylen-1;i++)
printf("%c",rem[i]);
getch();
}

****************************************************************
7)C Program to detect error using Checksum method.
#include<stdio.h>
#include<conio.h>
#include<math.h>
long int sender(int b[10],int k)
{
int i,sum=0,checksum;
printf("\n****SENDER****\n");
for(i=0;i<k;i++)
sum+=b[i];
printf("SUM IS: %d",sum);
checksum=~sum;
printf("\nSENDER's CHECKSUM IS:%d",checksum);
return checksum;
}

long int receiver(int c[10],int k,int scheck)


{
int i,sum=0,checksum;
printf("\n\n****RECEIVER****\n");
for(i=0;i<k;i++)
sum+=c[i];
printf(" RECEIVER SUM IS:%d",sum);
sum=sum+scheck;
checksum=~sum;
printf("\nRECEIVER's CHECKSUM IS:%d",checksum);
return checksum;
}
void main()
{
int a[10],b[10],i,m,scheck,rcheck;
clrscr();
printf("\nENTER SIZE OF THE STRING:");
scanf("%d",&m);
printf("\nENTER THE data to be sent:");
for(i=0;i<m;i++)
scanf("%d",&a[i]);
scheck=sender(a,m);
printf("\nData is been sent to the reciever\n");
printf("Enter data which has been recieved\n");
for(i=0;i<m;i++)
scanf("%d",&b[i]);
rcheck=receiver(b,m,scheck);
if(rcheck==0)
printf("\n\nNO ERROR IN TRANSMISSION\n\n");
else
printf("\n\nERROR DETECTED");
getch();
}
*****************************************************************

8) C program to perform encryption and decryption using RSA algorithm.


#include<stdio.h>
#include<math.h>
// Returns gcd of a and b
int gcd(long a,long h)
{
int rem;
while (1)
{
rem =a%h;
if (rem == 0)
return h;
a = h;
h = rem;
}
}
void main()
{
long p,q,n,phi,e=2,k=0,msg,d;
long c,m;
char msg1;
clrscr();
printf("\n\nEnter plain text to be encrypted\n ");
scanf("%ld",&msg);
//scanf("%c",&msg1);
//msg=msg1-97;
printf("Enter any two prime numbers\n");
scanf("%ld%ld",&p,&q);
n=p*q;
phi=(p-1)*(q-1);

while (e < phi)


{
if (gcd(e, phi)==1)
break;
else
e++;
}
// printf("\n\n phi=ld, k=%ld",phi,k);
printf("\nEncryption key is e = %ld and n = %ld\n", e,n);
c=pow(msg,e);
printf("\n\t\tvalue of c after pow fun:%ld",c);
c=c%n;
printf("\nEncrypted data(cipher text) = %ld",c);
while (k<e)
{
if((1+k*phi)%e==0)
break;
k++;
}
//printf("\nThe value of k=%ld",k);
d=(1+(k*phi))/e;
printf("\n\nDecryption key is d = %ld\n",d);
m=pow(c,d);
printf("\n\t\tvalue of m after power %ld",m);
m=m%n;
printf("\n Decrypted data(plain text) = %ld",m);
getch();
}

You might also like