-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverse_string.cpp
56 lines (43 loc) · 936 Bytes
/
reverse_string.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
// Copyright (c) 2019 David Davis. All rights reserved.
//
// Compiled on macOS with:
//
// clang++ -O2 -std=c++14 -o reverse_string reverse_string.cpp
//
static const char copyright[] __attribute((used)) =
"Copyright (c) 2019 David Davis. All rights reserved.";
#include <iostream>
#include <string>
#include <cstring>
void usage(const char* programName)
{
std::cout << "usage: " << programName << " string\n";
}
void reverse(char* string)
{
int stringLength;
int i;
stringLength = strlen(string);
for (i = 0; i < stringLength / 2; i++)
{
char temp;
temp = string[i];
string[i] = string[(stringLength - 1) - i];
string[(stringLength - 1) - i] = temp;
}
}
int main(int argc, const char** argv)
{
if (--argc == 1)
{
char* s = new char[strlen(argv[1]) + 1];
(void)strcpy(s, argv[1]);
reverse(s);
std::cout << "result: " << s << std::endl;
delete[] s;
}
else
usage(argv[0]);
return 0;
}