Skip to content

Instantly share code, notes, and snippets.

@y-fedorov
Last active May 22, 2018 09:18
Show Gist options
  • Save y-fedorov/ce846684251b951c504dc580e91df114 to your computer and use it in GitHub Desktop.
Save y-fedorov/ce846684251b951c504dc580e91df114 to your computer and use it in GitHub Desktop.
String Rotate
#include <vector>
#include <iostream>
#include <string>
std::string simpleRotate(const std::string data) {
return std::string(data.rbegin(), data.rend());
}
std::string badRotate(std::string data) {
auto size = data.size();
for (auto i = size; i > 0; i--) {
data.insert(size - i, 1, data.back());
data.pop_back();
}
return data;
}
std::string cLikeRotate(std::string data) {
int i = 0;
int j = data.size() - 1;
char tmp;
while (i < j)
{
tmp = data[i];
data[i] = data[j];
data[j] = tmp;
i++;
j--;
}
return data;
}
std::string defaultRotate(std::string data) {
std::reverse(data.begin(), data.end());
return data;
}
int main()
{
std::string data = "Hello, World!";
std::string correctResult = "!dlroW ,olleH";
auto result1 = simpleRotate(data);
auto result2 = badRotate(data);
auto result3 = cLikeRotate(data);
auto result4 = defaultRotate(data);
if (result1 != correctResult) {
std::cout << "Fail! ('simpleRotate' example)" << std::endl;
return EXIT_FAILURE;
}
if (result2 != correctResult) {
std::cout << "Fail! ('badRotate' example)" << std::endl;
return EXIT_FAILURE;
}
if (result3 != correctResult) {
std::cout << "Fail! ('cLikeRotate' example)" << std::endl;
return EXIT_FAILURE;
}
if (result4 != correctResult) {
std::cout << "Fail! ('defaultRotate' example)" << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment