From 09066b7cb3b6b9cb88a7458ad9ca0c5669325bbd 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:36:13 +0000 Subject: [PATCH] [Sync Iteration] cpp/crypto-square/1 --- .../cpp/crypto-square/1/crypto_square.cpp | 63 +++++++++++++++++++ solutions/cpp/crypto-square/1/crypto_square.h | 17 +++++ 2 files changed, 80 insertions(+) create mode 100644 solutions/cpp/crypto-square/1/crypto_square.cpp create mode 100644 solutions/cpp/crypto-square/1/crypto_square.h diff --git a/solutions/cpp/crypto-square/1/crypto_square.cpp b/solutions/cpp/crypto-square/1/crypto_square.cpp new file mode 100644 index 0000000..e450961 --- /dev/null +++ b/solutions/cpp/crypto-square/1/crypto_square.cpp @@ -0,0 +1,63 @@ +#include "crypto_square.h" +#include +#include + +namespace crypto_square { + +// Constructor +cipher::cipher(const std::string& text) { + process_encryption(text); +} + +void cipher::process_encryption(const std::string& text) { + // Caso: Si la entrada está vacía + if (text.empty()) { + m_cipher_text = ""; + return; + } + + // 1. Fase de Normalización + std::string normalized = ""; + for (char c : text) { + if (std::isalnum(c)) { + normalized += std::tolower(c); + } + } + + size_t L = normalized.length(); + if (L == 0) { + m_cipher_text = ""; + return; + } + + // 2. Cálculo de Dimensiones de la Matriz Geométrica + size_t c = std::ceil(std::sqrt(L)); + size_t r = (L + c - 1) / c; + + // 3. Relleno (Padding) para un bloque rectangular perfecto + size_t perfect_size = c * r; + if (normalized.length() < perfect_size) { + normalized.append(perfect_size - normalized.length(), ' '); + } + + // 4. Transposición y Segmentación por Columnas + std::string encoded = ""; + for (size_t col = 0; col < c; ++col) { + for (size_t row = 0; row < r; ++row) { + encoded += normalized[row * c + col]; + } + + if (col < c - 1) { + encoded += " "; + } + } + + m_cipher_text = encoded; +} + +// Getter +std::string cipher::normalized_cipher_text() const { + return m_cipher_text; +} + +} diff --git a/solutions/cpp/crypto-square/1/crypto_square.h b/solutions/cpp/crypto-square/1/crypto_square.h new file mode 100644 index 0000000..fd732ba --- /dev/null +++ b/solutions/cpp/crypto-square/1/crypto_square.h @@ -0,0 +1,17 @@ +#pragma once +#include + +namespace crypto_square { + +class cipher { +private: + std::string m_cipher_text; + + void process_encryption(const std::string& text); + +public: + explicit cipher(const std::string& text); + std::string normalized_cipher_text() const; +}; + +}