diff --git a/homework/insensitive-palindrom/CMakeLists.txt b/homework/insensitive-palindrom/CMakeLists.txt index 9b1a9753b..42670b70e 100644 --- a/homework/insensitive-palindrom/CMakeLists.txt +++ b/homework/insensitive-palindrom/CMakeLists.txt @@ -18,7 +18,7 @@ FetchContent_MakeAvailable(googletest) project(insensitivePalindrom) enable_testing() -add_executable(${PROJECT_NAME}-ut test.cpp) # add your cpp file here after test.cpp +add_executable(${PROJECT_NAME}-ut detector.cpp test.cpp) # add your cpp file here after test.cpp # if this is problematic take a look into CMakeLists.txt files in other exercises add_compile_options(${PROJECT_NAME}-ut -Wall -Wextra -Wconversion -pedantic -Werror) diff --git a/homework/insensitive-palindrom/build.bat b/homework/insensitive-palindrom/build.bat new file mode 100644 index 000000000..313a5243b --- /dev/null +++ b/homework/insensitive-palindrom/build.bat @@ -0,0 +1,9 @@ +RMDIR build /S /Q +mkdir build +cd build +cmake -G "MinGW Makefiles" .. +MinGW32-make + +insensitivePalindrom-ut.exe + +cd .. diff --git a/homework/insensitive-palindrom/detector.cpp b/homework/insensitive-palindrom/detector.cpp new file mode 100644 index 000000000..361ef4458 --- /dev/null +++ b/homework/insensitive-palindrom/detector.cpp @@ -0,0 +1,25 @@ +#include "detector.hpp" +#include +#include + +std::string toLowerAndRemoveSpecial(const std::string& input) { + std::string result; + result.reserve(input.size()); + + std::for_each(input.begin(), input.end(), [&result](char c) { + if (std::isalnum(static_cast(c))) { + result += std::tolower(static_cast(c)); + } + }); + + return result; +} + +bool is_palindrome(std::string word) { + std::string word_raw = toLowerAndRemoveSpecial(word); + std::string word_raw_reverse = word_raw; + + std::reverse_copy(word_raw.begin(), word_raw.end(), word_raw_reverse.begin()); + + return std::equal(word_raw.begin(), word_raw.end(), word_raw_reverse.begin(), word_raw_reverse.end()); +} \ No newline at end of file diff --git a/homework/insensitive-palindrom/detector.hpp b/homework/insensitive-palindrom/detector.hpp new file mode 100644 index 000000000..2c70c77a1 --- /dev/null +++ b/homework/insensitive-palindrom/detector.hpp @@ -0,0 +1,4 @@ +#pragma once +#include + +bool is_palindrome(std::string word); \ No newline at end of file diff --git a/homework/insensitive-palindrom/test.cpp b/homework/insensitive-palindrom/test.cpp index dc5803917..463785916 100644 --- a/homework/insensitive-palindrom/test.cpp +++ b/homework/insensitive-palindrom/test.cpp @@ -1,7 +1,6 @@ +#include "detector.hpp" #include "gtest/gtest.h" -// TODO: Includes - class InsensitivePalindromFixture : public ::testing::TestWithParam> { };