Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions unordered map.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <iostream>
#include <string>
#include <unordered_map>
#include <sstream>

int main() {
std::string text = "This is a sample text. This is a simple example.";

// Create an unordered_map to store word frequencies
std::unordered_map<std::string, int> wordFrequency;

// Tokenize the text into words
std::istringstream iss(text);
std::string word;

while (iss >> word) {
// Remove punctuation and convert to lowercase (you may need a more comprehensive approach)
for (char& c : word) {
if (std::ispunct(c)) {
c = ' ';
}
else {
c = std::tolower(c);
}
}

// Increment the frequency in the map
if (!word.empty()) {
wordFrequency[word]++;
}
}

// Display the word frequencies
for (const auto& pair : wordFrequency) {
std::cout << pair.first << ": " << pair.second << std::endl;
}

return 0;
}