CPP   28
reverse algorithm
Guest on 3rd February 2023 01:36:23 AM


  1. // Using the STL generic reverse algorithm with a string and an array
  2. #include <iostream>
  3. #include <string>
  4. #include <cassert>
  5. #include <algorithm> // For reverse algorithm
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.   cout << "Using reverse algorithm with a string" << endl;
  11.   string string1 = "mark twain";
  12.   reverse(string1.begin(), string1.end());
  13.   assert (string1 == "niawt kram");
  14.   cout << " --- Ok." << endl;
  15.  
  16.   cout << "Using reverse algorithm with an array" << endl;
  17.   char array1[] = "mark twain";
  18.   int N1 = strlen(array1);
  19.   reverse(&array1[0], &array1[N1]);
  20.   assert (string(array1) == "niawt kram");
  21.   cout << " --- Ok." << endl;
  22.   return 0;
  23. }

Raw Paste

Login or Register to edit or fork this paste. It's free.