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
4
using namespace
std;
5
6
/*
7
// iterative version
8
string 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?
21
string
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
31
int
main
()
32
{
33
string
s;
34
cin >> s;
35
36
cout <<
"String in reverse = "
<<
reverse
(s) << endl;
37
38
39
return
0;
40
41
}
reverse
string reverse(string s)
Definition
string_reverse.cpp:21
main
int main()
Definition
string_reverse.cpp:31
Module7
string_reverse.cpp
Generated on Sun Sep 29 2024 16:39:53 for COMP2113 by
1.12.0