From 26c490f404731d053a6205719b6246502c07b449 Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Sat, 14 Jun 2014 16:46:27 +0200 Subject: init --- algorithms/string_reverse.cc | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 algorithms/string_reverse.cc (limited to 'algorithms/string_reverse.cc') diff --git a/algorithms/string_reverse.cc b/algorithms/string_reverse.cc new file mode 100644 index 0000000..8b3e12c --- /dev/null +++ b/algorithms/string_reverse.cc @@ -0,0 +1,37 @@ +#include +#include + +using namespace std; + + +string rev(string &s) +{ + string res; + for (int i = s.size()-1; i >= 0; i--) { // unsigned not! + res += s[i]; + } + return res; +} + +void rev_inplace(string &s) +{ + char temp; + int j = s.size()-1; + for (int i = 0; i < j; i++, j--) { + temp = s[i]; + s[i] = s[j]; + s[j] = temp; + } +} + + +int main() +{ + string s = "Asdf"; + cout << rev(s) << endl << "---" << endl; + cout << s << endl; + rev_inplace(s); + cout << s << endl; + return 0; +} + -- cgit v1.2.3