Home / utk / cs102 / fa16 / labi / code_snippets / section2 / strings_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
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>

using namespace std;

const int STR_SIZE = 3;

void reverse_string(string [], int);

int main() {
	string str[] = {
		"Clara Nguyen",
		"Racecar",
		"aibohphobia"
	};

	reverse_string(str, STR_SIZE);

	for (int i = 0; i < STR_SIZE; i++) {
		cout << str[i] << endl;
	}
}

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