-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
411 lines (388 loc) · 7.52 KB
/
Copy pathSource.cpp
File metadata and controls
411 lines (388 loc) · 7.52 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include <iostream>
#include <fstream>
#include <string>
int menu();
void add_text();
void Display_file();
void Empty_file();
void Encrypt_file();
void Decrypt_file();
void Merge();
void num_word();
void num_char();
void num_line();
void search_word();
void search();
void upper();
void lower();
void first();
void save();
using namespace std;
int main()
{
while (true)
{
switch (menu())
{
case 1:add_text();
break;
case 2:Display_file();
break;
case 3:Empty_file();
break;
case 4:Encrypt_file();
break;
case 5:Decrypt_file();
break;
case 6:Merge();
break;
case 7:num_word();
break;
case 8:num_char();
break;
case 9:num_line();
break;
case 10:search_word();
break;
case 11:search();
break;
case 12:upper();
break;
case 13:lower();
break;
case 14:first();
break;
case 15:save();
break;
default:cout << "plz enter correct number";
break;
}
if (menu() == 16)
{
break;
}
}
}
int menu()
{
int num;
cout <<
"1.Add new text to the end of the file \n"
"2.Display the content of the file \n"
"3.Empty the file \n"
"4.Encrypt the file content \n"
"5.Decrypt the file content \n"
"6.Merge another file \n"
"7.Count the number of words in the file. \n"
"8.Count the number of characters in the file \n"
"9.Count the number of lines in the file \n"
"10.Search for a word in the file \n"
"11.Count the number of times a word exists in the file \n"
"12.Turn the file content to upper case. \n"
"13.Turn the file content to lower case. \n"
"14.Turn file content to 1st caps(1st char of each word is capital) \n"
"15.Save \n"
"16.Exit \n"
"Enter the option u want do in ur file: ";
cin >> num;
return num;
}
void add_text()
{
string words;
char filename[100];
cout << "plz enter your file name with .txt : ";
cin >> filename;
fstream file;
file.open(filename, ios::app);
if(file)
{
cout << "enter text u want add: ";
std::cin >> std::ws;
getline(file,words);
file << words;
}
file.close();
}
void Display_file()
{
fstream file;
char filename[100];
cout << "plz enter your file name with .txt : ";
cin >> filename;
file.open(filename, ios::in);
if (file)
{
string line;
while(getline(file, line))
{
cout << line << endl;
}
}
file.close();
}
void Empty_file()
{
fstream file;
char filename[100];
cout << "plz enter your file name with .txt : ";
cin >> filename;
file.open(filename, ios::out);
file.close();
}
void Encrypt_file()
{
string encryption_text, filename;
cout << "plz enter your file name with .txt : ";
cin >> filename;
fstream file;
file.open(filename.c_str());
while (getline(file, encryption_text, '\0'))
{
for (int i = 0; i < encryption_text.length(); i++)
{
if (encryption_text[i] == 10)
{
continue;
}
encryption_text[i] = encryption_text[i] + 1;
}
file >> encryption_text;
}
cout << endl;
file.close();
file.open(filename, ios::out);
file << encryption_text;
file.close();
}
void Decrypt_file()
{
string Decrypt_text, filename;
cout << " Enter The Name of File with .txt : ";
cin >> filename;
fstream file;
file.open(filename.c_str());
while (getline(file, Decrypt_text, '\0'))
{
for (int i = 0; i < Decrypt_text.length(); i++)
{
if (Decrypt_text[i] == 10)
{
continue;
}
Decrypt_text[i] = Decrypt_text[i] - 1;
}
file >> Decrypt_text;
}
cout << endl;
file.close();
file.open(filename, ios::out);
file << Decrypt_text;
file.close();
}
void Merge()
{ fstream f1;
fstream f2;
char car[100];
string filename, Merge_file;
cout << " Enter The Name of File with .txt : ";
cin >> filename;
cout << " Enter The Name of File u want merge with .txt : ";
cin >> Merge_file;
f1.open(filename, ios::in);
f2.open(Merge_file, ios::app);
while (!f1.eof())
{
f1.getline(car, 99, '\n');
f2 << car << endl;
}
}
void num_word()
{
fstream f1;
string car,filename;
long long counter = 0;
cout << " Enter The Name of File with .txt : ";
cin >> filename;
f1.open(filename, ios::in);
while (!f1.eof()) {
f1 >> car;
counter++;
}
cout << "number of words is: " << counter << endl;
}
void num_char()
{
fstream f1;
string filename;
char ch;
long long counter = 0;
cout << " Enter The Name of File with .txt : ";
cin >> filename;
f1.open(filename, ios::in);
while (!f1.eof()) {
f1.get(ch);
if (ch == ' ') {
continue;
}
else if (ch == '\n') {
continue;
}
else {
++counter;
}
}
cout << "number of letters is: " << counter - 1 << endl;
}
void num_line()
{
fstream f1;
string filename;
char car[100];
long long counter = 0;
cout << " Enter The Name of File with .txt : ";
cin >> filename;
f1.open(filename, ios::in);
while (!f1.eof()) {
f1.getline(car, 99, '\n');
++counter;
}
cout << "number of lines is: " << counter << endl;
}
void search_word()
{
fstream f1;
string word, car, filename;
long long counter = 0;
bool found = false;
cout << " Enter The Name of File with .txt : ";
cin >> filename;
cout << "enter the word you want to check: ";
cin >> word;
f1.open(filename, ios::in);
while (!f1.eof()) {
f1 >> car;
for (size_t i = 0; i < car.length() - 1; i++)
{
car[i] = toupper(car[i]);
}
for (size_t i = 0; i < word.length() - 1; i++)
{
word[i] = toupper(word[i]);
}
if (car == word) {
found = true;
break;
}
else {
continue;
}
}
if (found == true) {
cout << "its there.. you are right (+_+)\n";
}
else
{
cout << "its not here try another one..!";
}
}
void search() {
string word,filename;
string x;
int count;
count = 0;
cout << " Enter The Name of File with .txt : ";
cin >> filename;
cout << "Enter the word you want to search about";
cin >> word;
ifstream file;
file.open(filename);
while (!file.eof()) {
file >> x;
if (x == word) {
count++;
}
}
cout << "the number of existence in the file is "
<< count;
}
void upper() {
string upper_text,filename;
fstream file;
cout << " Enter The Name of File with .txt : ";
cin >> filename;
file.open(filename.c_str());
while (getline(file,upper_text , '\0'))
{
for (int i = 0; i < upper_text.length(); i++)
{
upper_text[i] = toupper(upper_text[i]);
}
file >> upper_text;
}
file.close();
file.open(filename, ios::out);
file << upper_text;
file.close();
}
void lower() {
string lower_text,filename;
fstream file;
cout << " Enter The Name of File with .txt : ";
cin >> filename;
file.open(filename.c_str());
while (getline(file, lower_text, '\0'))
{
for (int i = 0; i < lower_text.length(); i++)
{
lower_text[i] = tolower(lower_text[i]);
}
file >> lower_text;
}
file.close();
file.open(filename, ios::out);
file << lower_text;
file.close();
}
void first() {
string first_text,filename;
fstream file;
cout << "Enter The Name of File with .txt : ";
cin >> filename;
file.open(filename.c_str());
while (getline(file, first_text, '\0'))
{
for (int i = 0; i < first_text.length(); i++)
{
first_text[0] = toupper(first_text[0]);
if (first_text[i] == ' ')
{
first_text[i+1] = toupper(first_text[i+1]);
}
}
file >> first_text;
}
file.close();
file.open(filename, ios::out);
file << first_text;
file.close();
}
void save() {
char input[100];
string filename;
ifstream file;
cout << " Enter The Name of File with .txt : ";
cin >> filename;
file.open(filename);
fstream datafile; char fileName[81];
cout << "Enter the name of a file: ";
cin.getline(fileName, 81);
datafile.open(fileName, ios::out);
cout << "File " << fileName << " saved the data.\n";
while (!file.eof()) {
file.getline(input, 100);
datafile << input << endl;
}
file.close();
datafile.close();
}