Skip to content

Instantly share code, notes, and snippets.

@rr-codes
Last active March 10, 2022 02:34
Show Gist options
  • Save rr-codes/08a7ab8217555744d801f0aa019a59cc to your computer and use it in GitHub Desktop.
Save rr-codes/08a7ab8217555744d801f0aa019a59cc to your computer and use it in GitHub Desktop.
std::string reverse_domain2(const std::string& s) {
std::string result;
auto index = 0;
for (const auto& c : s) {
if (c == '.') {
index = 0;
result.insert(result.begin() + index, '.');
continue;
}
result.insert(result.begin() + index, c);
index++;
}
return result;
}
std::string reverse_domain3(std::string& copy) {
std::reverse(copy.begin(), copy.end());
auto start = 0;
auto size = copy.size();
for (auto i = 0; i <= size; i++) {
if (copy[i] != '.' && i != size) continue;
std::reverse(copy.begin() + start, copy.begin() + i);
start = i + 1;
}
return copy;
}
std::string reverse_domain(const std::string& s) {
std::stringstream ss(s);
std::string segment;
std::vector<std::string> breakList;
while (std::getline(ss, segment, '.')) {
breakList.push_back(segment);
}
std::string result;
auto size = breakList.size();
for (int i = size - 1; i >= 0; --i) {
result += breakList[i];
if (i != size - 1) {
result += ".";
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment