forked from vitalWord/interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.cpp
More file actions
65 lines (43 loc) · 1.16 KB
/
Copy path1.cpp
File metadata and controls
65 lines (43 loc) · 1.16 KB
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
56
57
58
59
60
61
62
63
64
65
// Разверните строку. Указатель reverse_string должен
// указывать на развернутую строку.
#define USE_VERSION 1
static const char* str = "The string!";
#if (USE_VERSION == 1)
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
void strreverse(char * aStr);
# define swap_char(left, right) { char _t = left; left = right; right = _t; }
# ifdef _MSC_VER
# define strdup _strdup
# endif
int main()
{
char* reverse_string = strdup(str); //strlen + alloc + memcpy
strreverse(reverse_string);
printf("%s\n", str);
printf("%s\n", reverse_string);
free(reverse_string);
return 0;
}
void strreverse(char * p)
{
char *q = p + strlen(p);
for(--q; p < q; ++p, --q)
swap_char(*p, *q);
}
#endif
#if (USE_VERSION == 2)
# include <iostream>
# include <algorithm>
// std::reverse
# include <string>
int main () {
std::string result(str);
std::reverse(result.begin(), result.end());
char* reverse_string = &result[0];
std::cout << "Original: " << str << std::endl;
std::cout << "Reversed: " << reverse_string << std::endl;
return 0;
}
#endif