diff --git a/Hashmaps/unordered map.cpp b/Hashmaps/unordered map.cpp new file mode 100644 index 00000000..469752b6 --- /dev/null +++ b/Hashmaps/unordered map.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +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 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; +} diff --git a/fibonacci.cpp b/fibonacci.cpp new file mode 100644 index 00000000..68944c96 --- /dev/null +++ b/fibonacci.cpp @@ -0,0 +1,24 @@ +#include + +int fibonacci(int n) { + if (n <= 1) { + return n; + } else { + return fibonacci(n - 1) + fibonacci(n - 2); + } +} + +int main() { + int n; + std::cout << "Enter the value of n to calculate the nth Fibonacci number: "; + std::cin >> n; + + if (n < 0) { + std::cout << "Please enter a non-negative integer.\n"; + } else { + int result = fibonacci(n); + std::cout << "The " << n << "th Fibonacci number is: " << result << std::endl; + } + + return 0; +}