-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrailingString.c
More file actions
60 lines (54 loc) · 1.04 KB
/
trailingString.c
File metadata and controls
60 lines (54 loc) · 1.04 KB
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
#include <stdio.h>
#include <string.h>
#define LEN 80
int isTrailing(char str[], char phrase[]) {
int strLen = strlen(str);
int phraseLen = strlen(phrase);
int i, j;
if (phraseLen > strLen) {
return 0;
}
for (i = phraseLen - 1, j = strLen - 1; i >= 0; i--, j--) {
if (str[j] != phrase[i]) {
return 0;
}
}
return 1;
}
int main(int argc, char *argv[]) {
FILE *fp;
char *line;
char phrase[LEN];
char str[LEN];
size_t len;
int i, j;
if (argc != 2) {
printf("Unsupported number of parameters passed. Exiting.\n");
return 1;
}
fp = fopen(argv[1], "r");
getline(&line, &len, fp);
while (!feof(fp)) {
i = 0;
j = 0;
while (line[i] != ',') {
str[i] = line[i];
i++;
}
str[i] = '\0';
i++;
while (line[i] != '\n' && line[i] != '\0') {
phrase[j] = line[i];
i++;
j++;
}
if (line[i] == '\0') {
continue;
}
phrase[j] = '\0';
printf("%d\n", isTrailing(str, phrase));
getline(&line, &len, fp);
}
fclose(fp);
return 0;
}