-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpattern_match_print_line.c
46 lines (44 loc) · 1.01 KB
/
pattern_match_print_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
//WAP to print each line of its input file that contains a particular pattern of string of characters
#include <stdio.h>
#define MAXLINE 1000 /* maximum input line length */
int getlline(char line[], int max);
int strindex(char source[], char searchfor[]);
char pattern[] = "ould";
/* pattern to search for */
/* find all lines matching pattern */
int main()
{
char line[MAXLINE];
int found = 0;
while (getlline(line, MAXLINE) > 0)
if (strindex(line, pattern) >= 0) {
printf("\n");
printf("%s", line);
found++;
}
printf("\n");
return found;
}
/* getline: get line into s, return length */
int getlline(char s[], int lim)
{
int c, i;
i = 0;
while (--lim > 0 && (c=getchar())!= EOF && c != '\n')
s[i++] = c;
if (c == '\n')
s[i++] = c;
s[i] = '\0';
return i;
}
/* strindex: return index of t in s, -1 if none */
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;
}
return -1;
}