Home / utk / cs102 / fa16 / labi / code_snippets / section2 / string_reverse.cpp
Directory Listing
isstream.cpp
namelist.txt
osstream.cpp
result.txt
string_reverse.cpp
strings_reverse.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

using namespace std;

void reverse_string(string &);

int main() {
	string str = "Clara Nguyen";
	reverse_string(str);

	cout << str << endl;
}

void reverse_string(string &str) {
	char temp;
	int end_pos;
	for (int i = 0; i < str.length() / 2; i++) {
		end_pos = str.length() - i - 1;
		temp   = str[i];
		str[i      ] = str[end_pos];
		str[end_pos] = temp;
	}
}