From 6aa18c083124ccad7d1281e0a2f64e8e5aef9dc1 Mon Sep 17 00:00:00 2001 From: Andrey Kassaev Date: Tue, 5 Mar 2024 17:15:45 +0400 Subject: [PATCH] init --- .gitignore | 2 ++ Main.cpp | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 .gitignore create mode 100644 Main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6df1069 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*/* +OOP* \ No newline at end of file diff --git a/Main.cpp b/Main.cpp new file mode 100644 index 0000000..2bed818 --- /dev/null +++ b/Main.cpp @@ -0,0 +1,67 @@ +#pragma once +#include +#include +#include +#include + +void tokenize(std::string str, std::vector& token_v); +bool isPalindrome(std::string str); +std::string replaceStr(std::string str, const std::string& from, const std::string& to); + +int main() { + + std::string str = "Hi how are you and WOW and abba"; + std::vector str_vector; + std::vector str_vector_pal; + int palindromeCounter = 0; + + std::cout << "Source string: " << str << "\n"; + std::cout << "Edited string: " << replaceStr(str, "and", ",and") << "\n\n"; + + tokenize(str, str_vector); + + for (std::string& str: str_vector) + { + if (isPalindrome(str)) + { + palindromeCounter += 1; + str_vector_pal.push_back(str); + } + } + + std::cout << "Palindrome Counter = " << palindromeCounter << "; "; + for (std::string& str: str_vector_pal) + { + std::cout << str << ", "; + } + std::cout << "\n"; + + system("pause"); + + return 0; +}; + +bool isPalindrome(std::string str) { + std::string tmp = str; + std::reverse(tmp.begin(), tmp.end()); + return (str == tmp) ? true : false; +} + +void tokenize(std::string str, std::vector& token_v) { + size_t start = str.find_first_not_of(' '), end = start; + + while (start != std::string::npos) { + end = str.find(' ', start); + token_v.push_back(str.substr(start, end - start)); + start = str.find_first_not_of(' ', end); + } +} + +std::string replaceStr(std::string str, const std::string& from, const std::string& to) { + size_t start_pos = 0; + while ((start_pos = str.find(from, start_pos)) != std::string::npos) { + str.replace(start_pos, from.length(), to); + start_pos += to.length(); + } + return str; +}