-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefixStat.c
125 lines (101 loc) · 3.27 KB
/
prefixStat.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define IN 1 /*inside a word*/
#define OUT 0 /*outside a word*/
int wordcount(FILE *fp);
int linecount(FILE *fp);
int main(int argc, char *argv[])
{
char help[] = "-h";
char line[] = "-l";
char word[] = "-w";
char pre[] = "-p";
if(argc < 2 || argc > 3){ /*check for correct number of input*/
fprintf(stderr, "Invalid input.\n");
}
else
{
FILE *fp;
fp = fopen("test.dat", "r");
if(fp == NULL) /*check for file error*/
{
fprintf(stderr, "Could not open file.\n");
}
else if(argc == 2)
{
if(strcmp(argv[1], word) == 0)
{
int c;
int nw = 1;
while ((c = fgetc(fp)) != EOF)
{
printf("%c", c);
if (c == ' ' || c == '\n' || c == '\t'){
nw++;
}
}
printf("\n%d", nw);
}
else if(strcmp(argv[1], line) == 0)
{
int c;
int nl = 1;
while ((c = fgetc(fp)) != EOF)
{
printf("%c", c);
if(c == '\n'){
nl++;
}
}
printf("\n%d", nl);
}
else if(strcmp(argv[1], help) == 0)
{
printf("Welcome. Type one of the following options:\n");
printf("'-w': output the words of your file and word count\n");
printf("'-l': output the lines of your file and line count\n");
printf("'-p <input word>': type in a prefix word to output the words of your file which contain that prefix\n");
}
else /*error if not -w, -l, or -h*/
{
fprintf(stderr, "Invalid input, please enter either -w, -h, or -l.\n");
}
}
else /*here there must be three arguments passed*/
{
if(strcmp(argv[1], pre) != 0)
{
fprintf(stderr, "Invalid input.\n");
}
else /*here we will execute the prefix function*/
{
char tempword[1000];
int length = strlen(argv[2]);
char prefix[length];
strcpy(prefix, argv[2]);
int i, c;
i = 0;
while((c = fgetc(fp)) != EOF)
{
if (c == ' ' || c == '\n' || c == '\t')
{
if (strncasecmp(prefix, tempword, length) == 0)
{
printf("%s ", tempword);
}
i = 0;
for(; i < 1000; i++)
{
tempword[i] = NULL;
}
continue;
}
tempword[i] = c;
i++;
/*printf("%c\n", tempword[i]);*/
}
}
}
}
}