-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline.c
53 lines (48 loc) · 954 Bytes
/
line.c
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
//WAP to print that line taken from an input file having a certain pattern
//Sir's Method
#include<stdio.h>
#include<time.h>
#define MAXLINE 10000
int getlines(char line[],int max);
int strindex(char source[],char searchfor[]);
char pattern[]="ould";
int main(){
char line[MAXLINE];
int found=0;
clock_t tic = clock();
while(getlines(line,MAXLINE)>0){
if(strindex(line,pattern)>=0){
printf("%s",line);
found++;
}
}
printf("%d\n",found);
clock_t toc = clock();
printf("Elapsed: %lf seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);
return 0;
}
int strindex(char s[],char t[]){
int i,j,k;
for(i=0;s[i]!='\0';i++){
for(j=i,k=0;t[k]!='\0' && s[j]==t[k];j++,k++);
if(k>0 && t[k]=='\0'){
return i;
}
}
// else{
return -1;
// }
// }
}
int getlines (char s[],int len){
int c,i;
for(i=0; i<=(len-1) && ((c=getchar())!=EOF) && c!='\n';++i){
s[i]=c;
}
if(c=='\n'){
s[i]=c;
++i;
}
s[i]='\0';
return i;
}