Skip to content
Open
Show file tree
Hide file tree
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
48 changes: 48 additions & 0 deletions solutions/cpp/word-count/1/word_count.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "word_count.h"
#include <cctype>

using namespace std;

namespace word_count {

map<string, int> words(const string& text) {
map<string, int> 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;
}

}
8 changes: 8 additions & 0 deletions solutions/cpp/word-count/1/word_count.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once
#include <string>
#include <map>

namespace word_count {
std::map<std::string, int> words(const std::string& text);

}