forked from jhpy1024/CProgrammingLanguageExercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise_4-1.c
66 lines (52 loc) · 1.13 KB
/
exercise_4-1.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
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdio.h>
#include <string.h>
#define MAX_LINE_LENGTH 1000
enum
{
NOT_FOUND = -1
};
int strrindex(char s[], char t[])
{
int rightmost_index = NOT_FOUND;
int substr_index = 0;
for (int i = 0; s[i] != '\0'; ++i)
{
for (int j = i, substr_index = 0; s[j] == t[substr_index]; ++j, ++substr_index)
{
if (substr_index == strlen(t) - 1)
{
rightmost_index = i;
break;
}
}
}
return rightmost_index;
}
int get_line(char str[], int max_line_length)
{
int current_char = EOF;
int index = 0;
while (--max_line_length > 0 && (current_char = getchar()) != EOF && current_char != '\n')
{
str[index++] = current_char;
}
if (current_char == '\n')
{
str[index++] = current_char;
}
str[index] = '\0';
return index;
}
int main()
{
char line[MAX_LINE_LENGTH] = { 0 };
char pattern[] = "ould";
while (get_line(line, MAX_LINE_LENGTH) > 0)
{
if (strrindex(line, pattern) != NOT_FOUND)
{
puts(line);
}
}
return 0;
}