-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
155 lines (139 loc) · 4.75 KB
/
main.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BUFFER_SIZE 1000
int checkFirstChar = 0;
int usedOption = 0;
int choosenOption = 0;
int lowerCaseCounter = 0;
int upperCaseCounter = 0;
int numberOfWordMatchesInFile = 0;
int programmResult = 0;
char *wordToFind;
char characterInFile;
char filename[512];
void printUsage(){
printf("\nUsage: ./main.c {-h for help|-b|-s|-c word} filename\n");
}
int validateUserInput(int firstInputChar, int secondInputChar){
if (firstInputChar == 45 && secondInputChar == 104){
return secondInputChar;
} else if (firstInputChar == 45 && secondInputChar == 98){
return secondInputChar;
} else if (firstInputChar == 45 && secondInputChar == 115){
return secondInputChar;
} else if (firstInputChar == 45 && secondInputChar == 99){
return secondInputChar;
} else {
printUsage();
exit(1);
}
}
void printHelp(){
printf("\n====================\t HELPPAGE \t==================\n");
printUsage();
printf("\nOPTIONS:\n");
printf("\t-h\t\tdisplay this page.\n");
printf("\t-b\t\tcount all upper cases in the choosen file.\n");
printf("\t-s\t\tcount all lowercases in the choosen file. \n");
printf("\t-c <word>\tcheck if word is in the file.\n");
printf("\t<filename>\tist the name of the file which you like to check.\n");
printf("\n====================\t HELPPAGE \t==================\n");
exit(1);
}
int countUpperCases(FILE *file){
int lowestUpperCase = 65;
int biggestUpperCase = 90;
while((characterInFile = getc(file)) != EOF ) {
if (characterInFile >= lowestUpperCase && characterInFile <= biggestUpperCase){
upperCaseCounter++;
}
}
return upperCaseCounter;
}
int countLowerCases(FILE *file){
int lowestLoewrCase = 97;
int biggestLowerCase = 122;
while((characterInFile = getc(file)) != EOF ) {
if (characterInFile >= lowestLoewrCase && characterInFile <= biggestLowerCase){
lowerCaseCounter++;
}
}
return lowerCaseCounter;
}
int wordIsInFile(FILE *file, char *wordWhichIsToFind){
char fileString[BUFFER_SIZE];
char *pos;
int index;
// Read line from file till end of file.
while ((fgets(fileString, BUFFER_SIZE, file)) != NULL){
index = 0;
// Find next occurrence of word in fileString
while ((pos = strstr(fileString + index, wordWhichIsToFind)) != NULL){
// Index of word in fileString is Memory address of pos - memory address of fileString.
index = (pos - fileString) + 1;
numberOfWordMatchesInFile++;
}
}
return numberOfWordMatchesInFile;
}
int main(int argc, char *argv[]) {
//check how many parametes are given and print help if to many
if (argc > 1 && argc < 5){
checkFirstChar = argv[1][0];
usedOption = argv[1][1];
}else{
printUsage();
exit(1);
}
//validate user input
choosenOption = validateUserInput(checkFirstChar, usedOption);
if (choosenOption == 104){
printHelp();
}
strcpy(filename, argv[argc-1]);
FILE *readFile = fopen(filename, "r");
if (readFile == NULL){
printf("Readable file not found!\n Correct path to file?? \n");
exit(1);
}
FILE *writeFile = fopen("result.txt", "w");
if (writeFile == NULL){
printf("Writeable file not found!\n Correct path to file?? \n");
exit(1);
}
//run chooses function
if (choosenOption == 98){
programmResult = countUpperCases(readFile);
char result[] = "Es befinden sich folgende Anzahl an Grossbuchstaben in der Datei: ";
fputs(result, writeFile);
fprintf(writeFile, "%d", programmResult);
printf("### write result to result.txt ###\n");
} else if (choosenOption == 115){
programmResult = countLowerCases(readFile);
char result[] = "Es befinden sich folgende Anzahl an Kleinbuchstaben in der Datei: ";
fputs(result, writeFile);
fprintf(writeFile, "%d", programmResult);
printf("### write result to result.txt ###\n");
} else if (choosenOption == 99){
if (argc > 2){
wordToFind = argv[2];
} else {
printUsage();
exit(1);
}
programmResult = wordIsInFile(readFile, wordToFind);
if (programmResult > 0){
char result[] = "Das Wort befindet sich folgende Anzahl in der Datei: ";
fputs(result, writeFile);
fprintf(writeFile, "%d", programmResult);
} else{
char result[] = "Das Wort befindet sich nicht in der Datei! ";
fputs(result, writeFile);
}
printf("### write result to result.txt ###\n");
}
fclose(readFile);
fclose(writeFile);
return 0;
}