-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7_1.cpp
More file actions
102 lines (67 loc) · 2.33 KB
/
7_1.cpp
File metadata and controls
102 lines (67 loc) · 2.33 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
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
void toLowerCase(char* str) {
for (int i = 0; str[i]; ++i)
str[i] = tolower(str[i]);
}
bool areEqual(const char* word1, const char* word2) {
return strcmp(word1, word2) == 0;
}
int main() {
const int MAX_PARAGRAPH_LENGTH = 1000;
char paragraph[MAX_PARAGRAPH_LENGTH];
cout << "Enter a paragraph:\n";
cin.getline(paragraph, MAX_PARAGRAPH_LENGTH);
toLowerCase(paragraph);
char** wordList = nullptr;
int* wordCount = nullptr;
int wordCapacity = 10;
int wordSize = 0;
wordList = new char*[wordCapacity];
wordCount = new int[wordCapacity];
const char* delimiters = " ,.;:!?\"'\n\t";
char* token = strtok(paragraph, delimiters);
while (token != nullptr) {
bool found = false;
for (int i = 0; i < wordSize; ++i) {
if (areEqual(wordList[i], token)) {
wordCount[i]++;
found = true;
break;
}
}
if (!found) {
if (wordSize >= wordCapacity) {
wordCapacity *= 2;
char** newWordList = new char*[wordCapacity];
int* newWordCount = new int[wordCapacity];
for (int i = 0; i < wordSize; ++i) {
newWordList[i] = wordList[i];
newWordCount[i] = wordCount[i];
}
delete[] wordList;
delete[] wordCount;
wordList = newWordList;
wordCount = newWordCount;
}
wordList[wordSize] = new char[strlen(token) + 1];
strcpy(wordList[wordSize], token);
wordCount[wordSize] = 1;
wordSize++;
}
token = strtok(nullptr, delimiters);
}
cout << "\nWord Frequency Count:\n";
for (int i = 0; i < wordSize; ++i) {
cout << wordList[i] << " : " << wordCount[i] << endl;
}
for (int i = 0; i < wordSize; ++i) {
delete[] wordList[i];
}
delete[] wordList;
delete[] wordCount;
cout<<endl<<"24CE052_pushtikansara"<<endl;
return 0;
}