From 3343eb9a08916e1b2e67ac96ae36bb788f0c276f Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Sun, 21 Jun 2026 21:11:22 +0000 Subject: [PATCH] [Sync Iteration] cpp/word-count/1 --- solutions/cpp/word-count/1/word_count.cpp | 48 +++++++++++++++++++++++ solutions/cpp/word-count/1/word_count.h | 8 ++++ 2 files changed, 56 insertions(+) create mode 100644 solutions/cpp/word-count/1/word_count.cpp create mode 100644 solutions/cpp/word-count/1/word_count.h diff --git a/solutions/cpp/word-count/1/word_count.cpp b/solutions/cpp/word-count/1/word_count.cpp new file mode 100644 index 0000000..54a7fdc --- /dev/null +++ b/solutions/cpp/word-count/1/word_count.cpp @@ -0,0 +1,48 @@ +#include "word_count.h" +#include + +using namespace std; + +namespace word_count { + +map words(const string& text) { + map counts; + string current_word = ""; + + for (size_t i = 0; i < text.length(); ++i) { + char c = text[i]; + + // 1. Si es alfanumérico, se acumula en minúsculas + if (isalnum(c)) { + current_word += tolower(c); + } + // 2. Si es un apóstrofo, validamos si es una contracción válida + else if (c == '\'') { + if (!current_word.empty() && i + 1 < text.length() && isalnum(text[i + 1])) { + current_word += c; + } else { + // Si no es válida actúa como separador de la palabra actual. + if (!current_word.empty()) { + counts[current_word]++; + current_word = ""; + } + } + } + // 3. Cualquier otro carácter actúa como un separador + else { + if (!current_word.empty()) { + counts[current_word]++; + current_word = ""; // Limpiamos la ventana para la siguiente palabra + } + } + } + + // Aduana de cierre: Si el texto terminó y quedó una palabra en el búfer + if (!current_word.empty()) { + counts[current_word]++; + } + + return counts; +} + +} diff --git a/solutions/cpp/word-count/1/word_count.h b/solutions/cpp/word-count/1/word_count.h new file mode 100644 index 0000000..affb47c --- /dev/null +++ b/solutions/cpp/word-count/1/word_count.h @@ -0,0 +1,8 @@ +#pragma once +#include +#include + +namespace word_count { + std::map words(const std::string& text); + +}