-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictionary.c
236 lines (229 loc) · 6.08 KB
/
dictionary.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<ctype.h>
struct Dictionary
{
char word[100];
char meaning[250];
} dictionary[1000];// Declare dictionary as an array of structs
int countwords = 0;
void files();
void searchword();
void insertword();
void deleteword();
int main() {
int ch;
char word[100], meaning[250];
files();
printf("Simple Dictionary\n");
while (1) {
printf("\nMenu:\n");
printf("1. Search Word\n");
printf("2. Insert Word\n");
printf("3. Delete Word\n");
printf("4. Exit\n");
printf("Enter your choice: ");
//To verify user is giving option in intergers
while((scanf("%d",&ch)!=1))
{
while(getchar()!='\n');
printf("Error in option ,Please type respective option.\n");
}
switch (ch) {
case 1:
searchword();
break;
case 2:
insertword();
break;
case 3:
deleteword();
break;
case 4:
printf("exiting the program\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
break;
}
}
return 0;
}
void files()
{
FILE *fp;
int i = 0;
countwords=0;
fp = fopen("dictionary.csv", "r");
if (fp == NULL)
{
printf("Error opening file dictionary\n");
return;
}
char line1[350]; // Increase buffer size to avoid potential overflows
while (fgets(line1, sizeof(line1), fp)) {
// Remove trailing newline if present
if (line1[strlen(line1) - 1] == '\n') {
line1[strlen(line1) - 1] = '\0';
}
if (sscanf(line1, "%[^,],%[^,]", dictionary[i].word, dictionary[i].meaning) == 2) {
i++;
countwords++;
} else {
printf("Error parsing line: %s\n", line1);
}
if (i >= 1000) {
printf("Dictionary full.\n");
break;
}
}
fclose(fp);
}
void searchword()
{
if(countwords==0)
{
printf("No words to search\n");
return;
}
char word[100];
do
{
search:
printf("Type the word to search\n");
scanf(" %s",word);
for (int i = 0; word[i] != '\0'; i++)
{
if (isalpha(word[i])==0)
{
printf("Invalid input: Contains non-alphabetic characters.\n");
goto search;
}
}
break;
}while(1);
printf("Searching for word %s...\n", word);
for (int i = 0; i < countwords; i++) {
if (strcasecmp(word, dictionary[i].word) == 0) {
printf("Word found!\n");
printf("Word: %s\nMeaning: %s\n", dictionary[i].word, dictionary[i].meaning);
return;
}
}
printf("Word not found.\n");
}
//to insert a new word to file
void insertword()
{
char word[100], meaning[250];
FILE *fp;
if (countwords >= 1000)
{
printf("Dictionary is full. Cannot insert more words.\n");
return;
}
fp = fopen("dictionary.csv", "a"); // Open in append mode
if (fp == NULL)
{
printf("Error: Could not open file dictionary.\n");
return;
}
do
{
insert:
printf("Type the word to insert\n");
scanf(" %s",word);
for (int i = 0; word[i] != '\0'; i++)
{
if (isalpha(word[i])==0)
{
printf("Invalid input: Contains non-alphabetic characters.\n");
goto insert;
}
}
for (int i = 0; i < countwords; i++)
{
if (strcasecmp(word, dictionary[i].word) == 0)
{
printf("word already exists. Please type a different word.\n");
goto insert;
}
}
break;
}while(1);
printf("Type the meaning.\n");
scanf(" %[^\n]",meaning);
fprintf(fp, "%s,%s\n", word, meaning);
printf("Word '%s' has been added to the dictionary.\n", word);
strcpy(dictionary[countwords].word, word); // Add the new word to the in-memory dictionary
strcpy(dictionary[countwords].meaning, meaning);
countwords++;
fclose(fp);
}
//to delete a word from the file
void deleteword()
{
if(countwords==0)
{
printf("No words to delete.\n");
return;
}
int i=0;
char word[100];
FILE *fp = fopen("dictionary.csv", "r");
if (fp==NULL)
{
printf("Error: Could not open file dictionary.csv.\n");
return;
}
FILE *fp1 = fopen("temp.csv", "w");
if (fp1==NULL)
{
printf("Error: Could not create temporary file.\n");
fclose(fp);
return;
}
do
{
delete:
printf("Type the word to delete\n");
scanf(" %s",word);
for (int i = 0; word[i] != '\0'; i++)
{
if (isalpha(word[i])==0)
{
printf("Invalid input: Contains non-alphabetic characters.\n");
goto delete;
}
}
break;
}while(1);
int found = 0;
while (fscanf(fp, "%[^,],%[^,]\n", dictionary[i].word, dictionary[i].meaning) == 2||i<countwords)
{
if (strcasecmp(dictionary[i].word, word) == 0)
{
printf("The word %s is deleted from dictionary\n",word);
found=1;
}
else
{
fprintf(fp1, "%s,%s\n", dictionary[i].word, dictionary[i].meaning);
}
i++;
}
fclose(fp);
fclose(fp1);
if (found==1)
{
remove("dictionary.csv");
rename("temp.csv", "dictionary.csv");
}
else
{
printf("The word '%s' was not found in the dictionary.\n", word);
remove("temp.csv");
}
files();
}