-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstrmanip1.cpp
56 lines (46 loc) · 1.31 KB
/
strmanip1.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
56
// {"category": "String", "notes": "Reverse string"}
#include <SDKDDKVer.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
using namespace std;
//------------------------------------------------------------------------------
//
// Given a string such as "The quick brown fox jumps over the lazy dog" write
// a function that returns the reverse of the string "god yzal eht revo spmuj
// xof nworb kciuq ehT".
//
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//
// Implementation
//
//------------------------------------------------------------------------------
void reverse(char* pString)
{
if (nullptr == pString)
{
return;
}
int length = strlen(pString);
if (length < 2)
{
return;
}
for (int i = 0; i < length / 2; i++)
{
swap(pString[i], pString[length - 1 - i]);
}
}
//------------------------------------------------------------------------------
//
// Demo execution
//
//------------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
char string[] = "The quick brown fox jumps over the lazy dog\0";
reverse(string);
cout << string << endl;
return 0;
}