diff --git a/homework/insensitive-palindrom/CMakeLists.txt b/homework/insensitive-palindrom/CMakeLists.txt index 9b1a9753b..c98f8f0bb 100644 --- a/homework/insensitive-palindrom/CMakeLists.txt +++ b/homework/insensitive-palindrom/CMakeLists.txt @@ -18,7 +18,9 @@ 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 +test.cpp +palindrome.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/palindrome.cpp b/homework/insensitive-palindrom/palindrome.cpp new file mode 100644 index 000000000..bf26a8756 --- /dev/null +++ b/homework/insensitive-palindrom/palindrome.cpp @@ -0,0 +1,19 @@ +#include "palindrome.h" + +bool isItLetter(char c) { + if (int(c) >= 97 && int(c) <= 122) + return true; + + return false; +} + +bool is_palindrome(std::string input) { + std::for_each(begin(input), end(input), [](char& c) { c = std::tolower(c); }); + input.erase(std::remove_if(begin(input), end(input), [](char c) { return !isItLetter(c); }), input.end()); + + for (int i = 0, j = input.size() - 1; i++, j--; i < j) { + if (input[i] != input[j]) + return false; + } + return true; +} diff --git a/homework/insensitive-palindrom/palindrome.h b/homework/insensitive-palindrom/palindrome.h new file mode 100644 index 000000000..67ad5f2c2 --- /dev/null +++ b/homework/insensitive-palindrom/palindrome.h @@ -0,0 +1,6 @@ +#pragma once +#include +#include + +bool isItLetter(char c); +bool is_palindrome(std::string input); diff --git a/homework/insensitive-palindrom/test.cpp b/homework/insensitive-palindrom/test.cpp index dc5803917..512438650 100644 --- a/homework/insensitive-palindrom/test.cpp +++ b/homework/insensitive-palindrom/test.cpp @@ -1,5 +1,5 @@ #include "gtest/gtest.h" - +#include "palindrome.h" // TODO: Includes class InsensitivePalindromFixture : public ::testing::TestWithParam> {