COMP2113
COMP2113_ENGG1340 Programming technologies and Computer programming II [Section 2BC] [2023]
Loading...
Searching...
No Matches
string_reverse.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <string>
3
4using namespace std;
5
6/*
7// iterative version
8string reverse( string s )
9{
10 string r = "";
11
12 for (int i = 0; i < s.length(); ++i)
13 r = s[i] + r;
14
15 return r;
16}
17*/
18
19
20// recursive version?
21string reverse(string s)
22{
23 if (s.length() < 2)
24 return s;
25 else
26 return s[s.length() - 1] + reverse(s.substr(0, s.length() - 1));
27 // string.substr(starting_index, length) usage
28}
29
30
31int main()
32{
33 string s;
34 cin >> s;
35
36 cout << "String in reverse = " << reverse(s) << endl;
37
38
39 return 0;
40
41}
string reverse(string s)
int main()