Advertisement
cd62131

reverse

Feb 18th, 2014
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <iterator>
  4. #include <string>
  5. using namespace std;
  6. void my_reverse(string& s, int c = 0) {
  7.   if (!c) c = (int) s.size() - 1;
  8.   for (int i = 0, j = c; i < j; i++, j--) {
  9.     char t = s[i];
  10.     s[i] = s[j];
  11.     s[j] = t;
  12.   }
  13. }
  14. int main() {
  15.   string s1 = "This is a test.";
  16.   string s2 = "I like C++.";
  17.   string s3 = s1;
  18.   string s4 = s2;
  19.   my_reverse(s1);
  20.   my_reverse(s2, 7);
  21.   reverse(s3.begin(), s3.end());
  22.   reverse(s4.begin(), s4.begin() + 7 + 1);
  23.   cout << s1 << endl;
  24.   cout << s2 << endl;
  25.   cout << s3 << endl;
  26.   cout << s4 << endl;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement