Solved: 15 - STD String

This commit is contained in:
2026-08-15 16:55:46 +03:00
parent 1aacfbbd1a
commit 8688803952
3 changed files with 43 additions and 6 deletions
@@ -1,10 +1,46 @@
#include "std_string.hpp"
#include <algorithm>
#include <locale>
#include <ranges>
#include <sstream>
namespace arrays {
auto to_uppercase(std::string text) -> std::string { (void)text; return {}; }
auto trim(const std::string& text) -> std::string { (void)text; return {}; }
auto replace_all(std::string text, char from, char to) -> std::string { (void)text; (void)from; (void)to; return {}; }
auto starts_with(const std::string& text, const std::string& prefix) -> bool { (void)text; (void)prefix; return false; }
auto to_uppercase(const std::string &text) -> std::string {
// Returns copy of the string without mutating the original string
auto s = std::string{text};
auto c_to_upper = [](const unsigned char c) { return std::toupper(c); };
std::ranges::transform(s, s.begin(), c_to_upper);
return s;
}
auto trim(const std::string& text) -> std::string {
// Returns a trimmed copy of the string
auto s = std::string{text};
auto is_not_space = [](const unsigned char c) { return !std::isspace(c); };
const auto first = std::ranges::find_if(s, is_not_space);
const auto last = std::ranges::find_if(s.rbegin(), s.rend(), is_not_space).base();
s.erase(last, s.end());
s.erase(s.begin(), first);
return s;
}
auto replace_all(const std::string &text, const char from, const char to) -> std::string {
// This one also copies the string
auto s = std::string{text};
std::ranges::replace(s, from, to);
return s;
}
auto starts_with(const std::string& text, const std::string& prefix) -> bool {
// Since C++20 you can use std::string::starts_with, but anyways.
if (text.size() < prefix.size()) return false;
// uz -> unsigned size_t
for (auto idx = 0uz; idx < prefix.size(); ++idx) {
if (text[idx] != prefix[idx]) return false;
}
return true;
}
} // namespace arrays