Solution: Reverse a String

Naive

#include <string>
#include <iostream>

using namespace std;



int main()
{
    string s;
    cin >> s;

    string reversed_s;

    for (int i=s.size()-1; i>=0; i--)
        reversed_s += s[i];

    cout << reversed_s << endl;

    return 0;
}

Using std::reverse<>

#include <string>
#include <iostream>
#include <algorithm>

using namespace std;


int main()
{
    string s;
    cin >> s;

    std::reverse(s.begin(), s.end());

    cout << s << endl;

    return 0;
}

Using std::reverse_copy<>

#include <string>
#include <iostream>
#include <algorithm>

using namespace std;


int main()
{
    string s("abc");

    string reversed_s(s.size(), '*');
    std::reverse_copy(s.cbegin(), s.cend(), reversed_s.begin());
    cout << reversed_s << endl;

    return 0;
}
cluster_cxx03 C++ cluster_cxx03_stl Standard Template Library cluster_cxx03_stl_exercises STL: Exercises cluster_cxx03_stl_exercises_reverse_string Exercise: Reverse A String cluster_cxx03_stl_algorithm Algorithms cluster_cxx03_functions_and_methods Functions and Methods cluster_cxx03_data_encapsulation Data Encapsulation cluster_cxx03_templates C++ Template Basics cluster_cxx03_exceptions Exceptions cluster_c The C Programming Language cluster_c_introduction Introduction cxx03_introduction Introduction c_introduction_installation Installation cxx03_introduction->c_introduction_installation cxx03_stl_basics Standard Template Library: Basics cxx03_templates_class_templates Class Templates cxx03_stl_basics->cxx03_templates_class_templates cxx03_stl_exercises_reverse_string_solution Solution: Reverse a String cxx03_stl_exercises_reverse_string_exercise Exercise: Reverse a String cxx03_stl_exercises_reverse_string_solution->cxx03_stl_exercises_reverse_string_exercise cxx03_stl_algorithm_reverse reverse<>: Reversing In-Place cxx03_stl_exercises_reverse_string_solution->cxx03_stl_algorithm_reverse cxx03_stl_algorithm_reverse_copy reverse_copy<>: Copying and Reversing cxx03_stl_exercises_reverse_string_solution->cxx03_stl_algorithm_reverse_copy cxx03_stl_algorithm_reverse->cxx03_stl_basics cxx03_stl_algorithm_reverse_copy->cxx03_stl_basics cxx03_functions_and_methods_methods Methods cxx03_functions_and_methods_overloading Overloading cxx03_functions_and_methods_methods->cxx03_functions_and_methods_overloading cxx03_functions_and_methods_references References cxx03_functions_and_methods_this this cxx03_functions_and_methods_references->cxx03_functions_and_methods_this cxx03_data_encapsulation_classes_objects Classes and Objects cxx03_functions_and_methods_overloading->cxx03_data_encapsulation_classes_objects cxx03_functions_and_methods_const const cxx03_functions_and_methods_this->cxx03_functions_and_methods_const cxx03_functions_and_methods_const->cxx03_functions_and_methods_methods cxx03_functions_and_methods_static static Methods cxx03_functions_and_methods_static->cxx03_functions_and_methods_references cxx03_functions_and_methods_operators Operator Overloading cxx03_functions_and_methods_operators->cxx03_functions_and_methods_static cxx03_data_encapsulation_classes_objects->cxx03_introduction cxx03_data_encapsulation_c Object Oriented Programming In Good Ol’ C cxx03_data_encapsulation_classes_objects->cxx03_data_encapsulation_c cxx03_data_encapsulation_c->cxx03_introduction cxx03_templates_function_templates Function Templates cxx03_templates_class_templates->cxx03_templates_function_templates cxx03_exceptions_try_catch try - catch cxx03_templates_function_templates->cxx03_exceptions_try_catch cxx03_exceptions_basics Basics cxx03_exceptions_try_catch->cxx03_exceptions_basics cxx03_exceptions_basics->cxx03_functions_and_methods_operators