You are on page 1of 50

110050131536

kushal panchal

PRACTICAL-1
AIM:IMPLEMENT A PROGRAM TO FIND FACTORIAL OF A
NUMBER.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,mul=1;
clrscr();
printf("\nEnter the number for finding factorial:");
scanf("%d",&n);
while(n>0)
{
mul=mul*n;
n--;
}
printf("\nThe factorial of a number is %d",mul);
getch();
}

OUTPUT:
Enter the number for findind factorial:10

The factorial of a number is 24320

Page

BIT(CSE-2S)

110050131536

kushal panchal

PRACTICAL-2
Aim: Implement Fibonacci series using recursion
Iteration-the formula.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a=1,b=1,c=0;
clrscr();
printf("\nHow many elements you want in fibonaci series:");
scanf("%d",&n);
printf("%d",a);
printf("%d",b);
while(n>0)
{
c=a+b;
printf("%d",c);
a=b;
b=c;
n--;
}
getch();
}

OUTPUT:
How many elements you want in fibonaci series:6
1123581321

Page

BIT(CSE-2S)

110050131536

kushal panchal

PRACTICAL -04
Aim: Implement binary search using divide and conquer.
Program Code :
#include<stdio.h>
#include<conio.h>
void main()
{
int n,m,i,j,a[100];
clrscr();
printf("Enter size of array : ");
scanf("%d",&m);
printf("\n Enter %d elements : ",m);
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
}
i=0;
j=m;
printf("\n Enter the element to Search : ");
scanf("%d",&n);
if(i==j)
{
printf("a[i]");
}
else
{
while(i<=j)
Page

BIT(CSE-2S)

110050131536

kushal panchal

{
m=(i+j)/2;
if(n==a[m])
{
printf("\n The position is -> %d",m);
break;
}
else if(n<a[m])
{
j=m-1;
if(a[j]==n)
{
printf("\n The posotion is -> %d",j);
break;
}
}
else
{
i=m+1;
if(a[i]==n)
{
printf("\n The position is -> %d",i);
break;
}
}
}
}
Page

BIT(CSE-2S)

110050131536

kushal panchal

getch();
}
Output :
The complexity of binary search is :
->In best case O(1)
->In average and worst case O(log n)

Page

BIT(CSE-2S)

110050131536

kushal panchal

PRACTICAL -05
Aim : Implement merge sort using divide and conquer.
Program Code:
#include<stdio.h>
#include<conio.h>
void Mergesort(int [],int ,int );
void Merge(int [],int ,int ,int );
void main()
{
int a[100],n,p,q,i,r;
clrscr();
printf("\n Enter the array range : ");
scanf("%d",&n);
p=1;
r=n;
printf("\n Enter %d Elements : ",n);

for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
Printf(\n The sorted array is : );
Mergesort(a,p,n);

for(i=1;i<=n;i++)
{
printf(" %d",a[i]);
Page

BIT(CSE-2S)

110050131536

kushal panchal

}
getch();
}
void Mergesort(int a[],int p,int r)
{
int q;
if(p<r)
{
q=(p+r)/2;
Mergesort(a,p,q);
Mergesort(a,q+1,r);
Merge(a,p,q,r);
}
}
void Merge(int a[],int p,int q,int r)
{
int n1,n2,i,j,k;
int L[100],R[100];
n1=q-p+1;
n2=r-q;
for(i=1;i<=n1;i++)
{
L[i]=a[p+i-1];
}
for(j=1;j<=n2;j++)
{
R[j]=a[q+j];
Page

BIT(CSE-2S)

110050131536

kushal panchal

}
L[n1+1]=1000;
R[n2+1]=1000;
i=1;
j=1;
for(k=p;k<=r;k++)
{
if(L[i]<=R[j])
{
a[k]=L[i];
i++;
}
else
{
a[k]=R[j];
j++;
}
}
}

Page

BIT(CSE-2S)

110050131536

kushal panchal

Output :
The complexity of merge sort in all cases is O(nlog n)

Page

BIT(CSE-2S)

110050131536

kushal panchal

PRACTICAL -06
Aim: Implement Quick sort using divide and conquer.
Program Code :
#include<stdio.h>
#include<conio.h>
void swap(int [],int ,int );
void quicksort(int [],int ,int );
int partition(int [],int ,int );
void main()
{
int a[100],i,n;
clrscr();
printf("\n Enter Array range : ");
scanf("%d",&n);
printf("\n Enter %d elements : ",n);
for (i=0; i<n; i++)
{
scanf ("%d", &a[i]);
}
quicksort(a,0,n-1);
printf("\n Sorted elements : ");
for (i=0; i<n; i++)
{
printf(" %d",a[i]);
}
getch();
}
Page

BIT(CSE-2S)

110050131536

kushal panchal

void swap(int a[],int l,int r)


{
int temp;
temp=a[l];
a[l]=a[r];
a[r]=temp;
}
void quicksort(int a[],int i,int j)
{
int P;
if(j>i)
{
P=partition(a,i,j);
quicksort(a,i,P-1);
quicksort(a,P+1,j);
}
}
int partition(int a[],int i,int j )
{
int l,r;
int P;
P=l=i;
P=a[i];
r=j;
while(l<r)
{
while(a[l]<=P)
Page

BIT(CSE-2S)

110050131536

kushal panchal

{ l++; }
while( a[r] > P )
{ r--; }
if(l<r)
{ swap(a,l,r); }
}
a[i]=a[r];
a[r]=P;
return r;
}
Output :

The complexity of Quick sort is :


->In best case and average case O(nlog n))
->In worst case O(n2)

Page

BIT(CSE-2S)

110050131536

kushal panchal

Practical : 07
Aim : Implement fractional knapsack using greedy method.
Program Code :
#include<stdio.h>
#include<conio.h>
void sort(int);
struct ks
{
int w,v,index;
float s;
}a[10];
void main()
{
int max,n,y,w=0,ctr=0,v=0,p=1;
clrscr();
printf("Enter max weight:");
scanf("%d",&max);
printf("enter no of elements:");
scanf("%d",&n);
for(y=0;y<n;y++)
{
printf("weight & value %d:",y+1);
scanf("%d %d",&a[y].w,&a[y].v);
a[y].s=a[y].v/a[y].w;
a[y].index=y+1;
}
sort(n);
Page

BIT(CSE-2S)

110050131536

kushal panchal

printf("\n\n\nindex\telement\tweight\tvalue");
while (ctr<n)
{
if(a[ctr].w+w<=max)
{
printf("\n%d\t%d\t%d\t%d",p++,a[ctr].index,a[ctr].w,a[ctr].v);
w=w+a[ctr].w;
v=v+a[ctr++].v;
}
else
{
v=v+((max-w)*a[ctr].v)/a[ctr].w;
printf("\n%d\t%d\t%d\t%d",p++,a[ctr].index,max-w,((maxw)*a[ctr].v)/a[ctr].w);
w=max;
ctr++;
}
}
printf("\n\nTotal:value=%d \n\nTotal:weight %d\n\n",v,w);
getch();
}
void sort(int n)
{
struct ks temp;
int i,j;
for(i=0;i<n;i++)
{

Page

BIT(CSE-2S)

110050131536

kushal panchal

for(j=0;j<n-1;j++)
{
if(a[j].s<a[j+1].s)
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}

Output :
->The Complexity of fractional knapsack is O(nlogn).

Page

BIT(CSE-2S)

110050131536

kushal panchal

Practical : 08
Aim : Implement Prims algorithm to find shortest path by greedy method.
Progeam Code :
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,min,mindis,cost[10][10],n,ne=1,visited[10]={0},u,v;
clrscr();
printf("Enter no. of nodes:");
scanf("%d",&n);

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("Enter cost between nodes %d & %d : ",i,j);
scanf("%d",&cost[i][j]);

if(cost[i][j]==0)
{
cost[i][j]=999;
}
}
}
printf("matrix :\n ");
for(i=0;i<n;i++)
Page

BIT(CSE-2S)

110050131536

kushal panchal

{
for(j=0;j<n;j++)
{
printf("\t%d",cost[i][j]);
}
printf("\n");
}
visited[1]=1;
while(ne<n)
{
for(i=1,min=999;i<n;i++)
{
for(j=0;j<n;j++)
{
if(cost[i][j]<min)
{
if(visited[i]!=0)
{
min=cost[i][j];
u=i;
v=j;
}
}
}
if(visited[u]==0 || visited[v]==0)
{
visited[v]=1;
Page

BIT(CSE-2S)

110050131536

kushal panchal

mindis=mindis+min;
printf("\nedge %d to %d cost %d",u,v,cost[u][v]);
ne++;
cost[u][v]=999;
cost[v][u]=999;
}
}
}
getch();
}

Output :
->The complexity of Prims algorithm is O(n2)

Page

BIT(CSE-2S)

110050131536

kushal panchal

Practical : 09
Aim:Implement Breadth first search.
Program Code :
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 20
#define TRUE 1
#define FALSE 0
int g[size][size];
int visit[size];
int Q[size];
int front,rear;
int n;
void main()
{
int v1,v2;
char ans='y';
void create(),bfs(int v2);
clrscr();
create();
printf("\nThe adjacency matrix for the graph is\n");
for(v1=0;v1<n;v1++)
{
for(v2=0;v2<n;v2++)
{
printf("\t%d",g[v1][v2]);
Page

BIT(CSE-2S)

110050131536

kushal panchal

}
printf("\n");
}
getch();
do
{
for(v1=0;v1<n;v1++)
{
visit[v1]=FALSE;
}

printf("\nEnter the vertex from which you waant to traverse:");


scanf("%d",&v1);
if(v1>=n)
{
printf("\nInvalid vertex");
}
else
{
printf("\nBreadth first search of graph is\n");
bfs(v1);
getch();
}
printf("\nDo you want to traverse from any other node?(y/n)\n : ");
ans=getche();
}while(ans=='y');
exit(0);
Page

BIT(CSE-2S)

110050131536

kushal panchal

}
void create()
{
int v1,v2;
char ans='y';
printf("\nEnter no. of nodes:");
scanf("%d",&n);
for(v1=0;v1<n;v1++)
{
for(v2=0;v2<n;v2++)
{
g[v1][v2]=FALSE;
}
}
printf("Enter vertices no. starting from 0:");
do
{
printf("\nEnter vertices v1 and v2:");
scanf("%d %d",&v1,&v2);
if(v1>=n || v2>=n)
{
printf("\nInvalid vertex value\n");
}
else
{
g[v1][v2]=TRUE;
g[v2][v1]=TRUE;
Page

BIT(CSE-2S)

110050131536

kushal panchal

}
printf("Add more edges?(y/n) : ");
ans=getche();
}while(ans=='y');
}
void bfs(int v1)
{
int v2;
visit[v1]=TRUE;
front=rear=-1;
Q[++rear]=v1;
while(front!=rear)
{
v1=Q[++front];
printf("\t%d",v1);
for(v2=0;v2<n;v2++)
{
if(g[v1][v2]==TRUE && visit[v2]==FALSE)
{
Q[++rear]=v2;
visit[v2]=TRUE;
}
}
}
}

Page

BIT(CSE-2S)

110050131536

kushal panchal

Output :
->The complexity of breath first search is O(|V|+|E|).

Page

BIT(CSE-2S)

110050131536

kushal panchal

Practical : 10
Aim: Implement Depth first search.
Program Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define m 20
#define TRUE 1
#define FALSE 0
int g[m][m];
int v[m];
int n;
void main()
{
int v1,v2;
char ans;
void create();
void dfs(int v2);
clrscr();
create();

printf("\nThe adjacency matrix for the graph is\n");


for(v1=0;v1<n;v1++)
{
for(v2=0;v2<n;v2++)
{
printf("\t%d",g[v1][v2]);
Page

BIT(CSE-2S)

110050131536

kushal panchal

}
printf("\n");
}
getch();
do
{
for(v1=0;v1<n;v1++)
{
v[v1]=FALSE;
}
printf("\nEnter the vertex from which you waant to traverse:");
scanf("%d",&v1);
if(v1>=n)
{
printf("\nInvalid vertex");
}
else
{
printf("\nDepth first search of graph is\n");
dfs(v1);
}
printf("\nDo you wannax to traverse from any other node?(y/n)");
ans=getch();
}while(ans=='y');
}
void create()
{
Page

BIT(CSE-2S)

110050131536

kushal panchal

int ch,v1,v2,flag;
char ans='y';
clrscr();
printf("\nEnter no. of nodes:");
scanf("%d",&n);
for(v1=0;v1<n;v1++)
{
for(v2=0;v2<n;v2++)
{
g[v1][v2]=FALSE;
}
}
printf("Enter vertices no. starting from 0:");
do
{
printf("\nEnter vertices v1 and v2:");
scanf("%d %d",&v1,&v2);
if(v1>=n || v2>=n)
{
printf("\nInvalid vertex value\n");
}
else
{
g[v1][v2]=TRUE;
g[v2][v1]=TRUE;
}
printf("Add more edges?(y/n)");
Page

BIT(CSE-2S)

110050131536

kushal panchal

ans=getche();
}while(ans=='y');
}
void dfs(int v1)
{
int v2;
printf("\t%d",v1);
v[v1]=TRUE;
for(v2=0;v2<m;v2++)
{
if(g[v1][v2]==TRUE && v[v2]==FALSE)
{
dfs(v2);
}
}
}

Page

BIT(CSE-2S)

110050131536

kushal panchal

Output :
->The complexity of depth first search algorithm is O(|V|+|E|).

Page

BIT(CSE-2S)

110050131536

kushal panchal

Practical : 11
Aim :Implement Chain matrix multiplication using Dynamic
Programming.
Program Code :
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void optimal(long **s,int i,int j)
{
if(i==j)
printf("A%d",i);
else
{
printf("(");
optimal(s,i,s[i][j]);
optimal(s,s[i][j]+1,j);
printf(")");
}
}
void main()
{
long l,i,j,k,n,*p,q,**m,**s;
clrscr();
printf("No of matrices:");
scanf("%ld",&n);
p=(long *)malloc((n+1)*4);
for(i=0;i<n+1;i++)
Page

BIT(CSE-2S)

110050131536

kushal panchal

{
printf("Enter value %ld of p:",i);
flushall();
scanf("%ld",&p[i]);
if(i>0){
printf("\tSize of A%ld matrix is %ld*%ld\n",i,p[i-1],p[i]);
}
}
s=(long **)malloc((n+1)*4);
m=(long **)malloc((n+1)*4);
for(i=0;i<n+1;i++){
m[i]=(long *)malloc((n+1)*4);
s[i]=(long *)malloc((n+1)*4);
}
for(i=1;i<=n;i++)
{
m[i][i]=0;
}
for(i=1;i<n+1;i++)
{
for(j=1;j<n+1;j++)
{

m[i][j]=0;
s[i][j]=0;
//printf("%ld ",m[i][j]);

}
//printf("\n");
}
Page

BIT(CSE-2S)

110050131536

kushal panchal

for(l=2;l<=n;l++)
{
for(i=1;i<=n-l+1;i++)
{
j=i+l-1;
m[i][j]=50000;
q=0;
for(k=i;k<=j-1;k++)
{
q=m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j];
printf("%ld\t",q);
if(q<m[i][j])
{
m[i][j]=q;
s[i][j]=k;
}
}
}
}
printf("m:\n");
for(i=1;i<n+1;i++)
{
for(j=1;j<n+1;j++)
{
printf("%ld\t ",m[i][j]);

Page

BIT(CSE-2S)

110050131536

kushal panchal

}
printf("\n");
}
printf("s:\n");
for(i=1;i<n+1;i++)
{
for(j=1;j<n+1;j++)
{
printf("%ld\t ",s[i][j]);
}
printf("\n");
}
optimal(s,1,n);
getch();
}

Page

BIT(CSE-2S)

110050131536

kushal panchal

Output :
->The complexity of chain matrix multiplication is O(n3).

Page

BIT(CSE-2S)

110050131536

kushal panchal

Practical :12
Aim :Implement Longest common subsequence using Dynamic
Programming.
Program :
#include<stdio.h>
#include<conio.h>
#include<string.h>
char x[10],z[10],b[10][10];
int c[10][10];
int m,n;
void print_lcs(int,int);
void main()
{
int i,j;
clrscr();
printf("enter the string x: ");
scanf("%s",x);
m=strlen(x);
printf("enter the string z: ");
scanf("%s",z);
n=strlen(z);
for(i=0;i<=m+1;i++)
{
for(j=0;j<=n+1;j++)
{
c[i][j]=0;
b[i][j]='0';
}
}
for(i=1;i<=m+1;i++)
{
for(j=1;j<=n+1;j++)
{
if(x[i-1]==z[j-1])
{
c[i][j]=c[i-1][j-1]+1;
b[i][j]='*';
}
else if(c[i-1][j]>=c[i][j-1])
{
Page

BIT(CSE-2S)

110050131536

kushal panchal
c[i][j]=c[i-1][j];
b[i][j]='!';
}
else
{
c[i][j]=c[i][j-1];
b[i][j]='#';
}

}
}
for(i=0;i<=m;i++)
{
for(j=0;j<=n;j++)
printf("%c\t",b[i][j]);
printf("\n");
}
printf("\n \n");
for(i=0;i<=m;i++)
{
for(j=0;j<=n;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
print_lcs(m,n);
getch();
}
void print_lcs(int i,int j)
{
if(i==0 || j==0)
return;
else if(b[i][j]=='*')
{
print_lcs(i-1,j-1);
printf("%c",x[i-1]);
}
else if(b[i][j]=='!')
print_lcs(i-1,j);
else
print_lcs(i,j-1);
}

Page

BIT(CSE-2S)

110050131536

kushal panchal

Output:
->Complexity of longest common subsequence program is O(nm).

Page

BIT(CSE-2S)

110050131536

kushal panchal

Practical : 13
Aim :Implement knapsack problem using dynamic programming.
Program :
#include<stdio.h>
#include<conio.h>
void Final_KP(void);
int wet[20],val[20],n,k[25][25],max,w,i;
void main()
{
clrscr();
printf("ENTER THE NO OF ITEMS:");
scanf("%d",&n);

for(i=1;i<n+1;i++)
{
printf("\nENTER THE VALUE[%d] WEIGHT[%d] : ",i,i);
scanf("%d %d",&val[i],&wet[i]);
}
printf("\nENTER THE MAXIMUM WEIGHT:");
scanf("%d",&max);
for(w=0;w<=max;w++)
{
k[0][w]=0;
}
for(i=1;i<=n;i++)

Page

BIT(CSE-2S)

110050131536

kushal panchal

{
k[i][0]=0;
}
for(i=1;i<=n;i++)
{
for(w=1;w<=max;w++)
{
if(wet[i]<=w)
{
if((val[i]+k[i-1][w-wet[i]])>k[i-1][w])
{
k[i][w]=val[i]+k[i-1][w-wet[i]];
}
else
{
k[i][w]=k[i-1][w];
}
}
if(wet[i]>w)
{
k[i][w]=k[i-1][w];
}
}
}
for(i=1;i<=n;i++)
{
for(w=0;w<=max;w++)
Page

BIT(CSE-2S)

110050131536

kushal panchal

{
printf(" %d",k[i][w]);
}
printf("\n");
}
Final_KP();
getch();
}
void Final_KP(void)
{ int kk,totv=0,totw=0;
i=n;
kk=max;
while(i>0 && kk>0)
{
if(k[i][kk]!=k[i-1][kk])
{
printf("\nTHE CHOOSEN ITEM = %d Weight=%d
Value=%d",i,wet[i],val[i]);
totv+=val[i];
totw+=wet[i];
i--;
kk=kk-wet[i];
}
else
{
i--;
}}

Page

BIT(CSE-2S)

110050131536

kushal panchal

printf("\n\nTHE TOTAL VALUE IS:%d",totv);


printf("\nTHE TOTAL WEIGHT IS:%d",totw);}
Output :
->The complexity of 0 1 Knapsack algorithm is O(2n).

Page

BIT(CSE-2S)

110050131536

kushal panchal

Practical : 14
Aim : Implement 4-Queens problem using backtracking. Only one solution
vector is sufficient.
Program :
#include<stdio.h>
#include<conio.h>
int select(int m,int n,int i,int j)
{
if((i-m)==(j-n) || (i-m)==(n-j) || n==j || m==i )
return 1;
return 0;
}
void main()
{
int i,j,c1,c2,c3,c4,l;
clrscr();
for(c1=0;c1<4;c1++)
{

i=0;
j=c1;
for(c2=0;c2<4;c2++)
{
i=1;
j=c2;

Page

BIT(CSE-2S)

110050131536

kushal panchal

if(select(0,c1,i,j)==1)
{ continue; }

for(c3=0;c3<4;c3++)
{
i=2,j=c3;
if(select(0,c1,i,j)==1 || select(1,c2,i,j)==1)
{ continue; }
for(c4=0;c4<4;c4++)
{
i=3;j=c4;
if(select(0,c1,i,j)==1 || select(1,c2,i,j)==1 ||
select(2,c3,i,j)==1)
{ continue; }
printf("(0,%d) (1,%d) (2,%d)
(3,%d)\n",c1,c2,c3,c4);
}
}
}
}
getch();}

Page

BIT(CSE-2S)

110050131536

kushal panchal

Output :
->The complexity of 4-Queens algorithm is O(n2).

Page

BIT(CSE-2S)

110050131536

kushal panchal

Practical : 15
Aim : Implement The nave algorithm-The Rabin Karp algorithm for
string matching.
Program :
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
#include<alloc.h>
void rabin();
void autom(char *t)
{
char p[]="abc";
int i,j,n=strlen(t),table[4][3],state=0;
table[0][0]=1;
table[0][1]=0;
table[0][2]=0;
table[1][0]=1;
table[1][1]=2;
table[1][2]=0;
table[2][0]=1;
table[2][1]=0;
table[2][2]=3;
table[3][0]=1;
Page

BIT(CSE-2S)

110050131536

kushal panchal

table[3][1]=0;
table[3][2]=0;

for(i=0;i<n;i++)
{
state=table[state][t[i]-97];
if(state==3)
printf("Pattern abc matches at %d\n",i-2);
}
}
void naive(char *t,char *p)
{
int n=strlen(t),m=strlen(p),s,i,flag=0;
for(s=0;s<=n-m;s++)
{

flag=0;
for(i=0;i<m;i++)
{
if(p[i]!=t[s+i])
flag=1;
}
if(flag==0)
{
printf("Match at %d\n",s);
}

}
}
void main()
Page

BIT(CSE-2S)

110050131536
{

kushal panchal

int i,j,n,m;
char *t,*p;
clrscr();
printf("Enter length of Text: ");
scanf("%d",&n);
printf("Enter length of pattern:");
scanf("%d",&m);
t=(char *)malloc(n);
p=(char *)malloc(m);
printf("Enter Text:");
scanf("%s",t);
printf("Enter Pattern:");
scanf("%s",p);
printf("RESULT OF NAIVE IS:\n");
TimeDifference(1);
naive(t,p);
TimeDifference(0);
printf("\n");
printf("RESULT OF AUTOMATA:\n");
TimeDifference(1);
autom(t);
TimeDifference(0);

printf("ENTER DETAILS FOR RABIN:\n");


TimeDifference(1);
rabin();
TimeDifference(0);
getch();
Page

BIT(CSE-2S)

110050131536

kushal panchal

void rabin()
{
int len,*p,len1,mod,str,count=0,sum = 0,pow10 = 0,modestr = 0,modesum = 0;
int *digit,numofdigit;
int temp,i,j,k,l;
printf("Enter the length of the whole string");
scanf("%d",&len);
p=(int*)malloc(len*sizeof(int));
for(i=0;i<len;i++)
{
printf("enter no");
scanf("%d",&p[i]);
}
printf("\n enter string to match");
scanf("%d",&str);
modestr = str % 13;
temp = str;
while(temp>0)
{
temp=temp/10;
count++;
}
digit = (int *)malloc(sizeof(int) * count);
Page

BIT(CSE-2S)

110050131536

kushal panchal

numofdigit = count;
temp = str;
int ExCount = numofdigit - 1;
while(temp > 0)
{
digit[ExCount--] = temp % 10;
temp = temp / 10;
}
int AllSame = 0;
pow10 = numofdigit - 1;
for(k=0;k<len-count;k++)
{
for(l=0;l<count;l++)
{
sum+=(pow(10,pow10--) * p[l+k]);
}
pow10 = numofdigit - 1;
modesum = sum % 13;
sum=0;
if(modestr == modesum)
{
int same =1;
for(l = 0 ; l < count ; l++)
{
if(digit[l] != p[l+k])
{
same = 0;
Page

BIT(CSE-2S)

110050131536

kushal panchal
break;
}

}
if(same == 1)
{
printf("\nString Found at %d",k+1);
AllSame = 1;
}
}
}
if(AllSame == 0)
{
printf("No matching string is Found.");
}
}

Page

BIT(CSE-2S)

110050131536

kushal panchal

Output :
->The complexity of the nave algorithm is O(2n).

Page

BIT(CSE-2S)

You might also like