-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7_2.cpp
More file actions
90 lines (62 loc) · 1.96 KB
/
7_2.cpp
File metadata and controls
90 lines (62 loc) · 1.96 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
#include <iostream>
#include <fstream>
#include <cstring>
#include <cctype>
using namespace std;
int countWords(const char* line) {
int wordCount = 0;
bool inWord = false;
for (int i = 0; line[i] != '\0'; ++i) {
if (isspace(line[i])) {
inWord = false;
} else if (!inWord) {
inWord = true;
wordCount++;
}
}
return wordCount;
}
int main() {
const int MAX_LINE_LENGTH = 1024;
char filename[100];
cout << "Enter the filename to analyze: ";
cin.getline(filename, 100);
ifstream file(filename);
if (!file.is_open()) {
cerr << "Error: Could not open file \"" << filename << "\". Please check if it exists and you have permission to read it.\n";
return 1;
}
int capacity = 10;
int size = 0;
char** lines = new char*[capacity];
int totalChars = 0;
int totalWords = 0;
int totalLines = 0;
char buffer[MAX_LINE_LENGTH];
while (file.getline(buffer, MAX_LINE_LENGTH)) {
totalLines++;
totalChars += strlen(buffer);
totalWords += countWords(buffer);
if (size >= capacity) {
capacity *= 2;
char** newLines = new char*[capacity];
for (int i = 0; i < size; ++i)
newLines[i] = lines[i];
delete[] lines;
lines = newLines;
}
lines[size] = new char[strlen(buffer) + 1];
strcpy(lines[size], buffer);
size++;
}
file.close();
cout << "\n--- File Statistics ---\n";
cout << "Total Characters: " << totalChars << endl;
cout << "Total Words : " << totalWords << endl;
cout << "Total Lines : " << totalLines << endl;
for (int i = 0; i < size; ++i)
delete[] lines[i];
delete[] lines;
cout<<endl<<"24CE052_Pushtikansara"<<endl;
return 0;
}