forked from portfoliocourses/cplusplus-example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path78_reverse_a_string.cpp
More file actions
42 lines (33 loc) · 1.06 KB
/
Copy path78_reverse_a_string.cpp
File metadata and controls
42 lines (33 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*******************************************************************************
*
* Program: Reverse A String Using reverse()
*
* Description: Example of how to reverse a string in C++ using the reverse()
* function of the algorithm library.
*
* YouTube Lesson: https://www.youtube.com/watch?v=4ysRvfzjmhQ
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
using namespace std;
int main()
{
// create a test string
string text1 = "There is no hand to catch time";
// we call reverse and pass it the range of the entire string
reverse(text1.begin(), text1.end());
// output the reversed string
cout << text1 << endl;
// create a C-style test string
char text2[] = "All that glitters is not gold";
// we use pointer arithmetic to pass it the range of the entire string
reverse(text2, text2 + strlen(text2));
// output the reversed string
cout << text2 << endl;
return 0;
}