CPP   24
reverse string
Guest on 7th February 2023 02:06:01 AM


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

Raw Paste

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