-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.cpp.CPP
More file actions
63 lines (61 loc) · 995 Bytes
/
3.cpp.CPP
File metadata and controls
63 lines (61 loc) · 995 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
int mystr(char [],char [],int,int);
void main()
{
char text[80],pat[80];
int tsize,psize,r;
clock_t start,end;
clrscr();
randomize();
//cout<<"\nEnter a text\n";
for(int i=0;i<80;i++)
{
int rc;
l:rc=rand()%124;
if((rc>=65&&rc<=91)||(rc>=97&&rc<=122))
{
text[i]=(char)rc;
}
else
{
goto l;
}
}
tsize=strlen(text);
cout<<"randomized text is\n\n";
cout<<text;
cout<<"\nEnter a pattern to be searched\n";
gets(pat);
psize=strlen(pat);
start=clock();
r=mystr(text,pat,tsize,psize);
end=clock();
if(r==-1)
{
cout<<"Pattern not found\n";
}
else
{
cout<<"Pattern found in position="<<(r+1);
}
printf("\nTime required to execute=%lf",(end-start)/CLK_TCK);
getch();
}
int mystr(char t[],char p[],int n,int m)
{
int i,j;
for(i=0;i<=(n-m);i++)
{
j=0;
while(j<m&&p[j]==t[i+j])
j=j+1;
if(j==m)
return i;
}
return -1;
}