-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotatestring.cpp
More file actions
50 lines (47 loc) · 855 Bytes
/
Rotatestring.cpp
File metadata and controls
50 lines (47 loc) · 855 Bytes
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
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
void rotatestring(string & a, int num)
{
int n = a.length();
if(n == 0 || num <= 0)
return;
int m = num % n;
if(0 == m)
return;
int p1 = 0;
int p2 = m;
int k = (n - m) - n % m;
int r = n % m;
while(k--)
{
swap(a[p1],a[p2]);
p1++;
p2++;
}
while(r--)
{
int i = p2;
while(i > p1)
{
swap(a[i],a[i-1]);
i--;
}
p1++;
p2++;
}
}
int main()
{
cout << "Hello world!" << endl;
// string a = "abcdefg";
cout << "Please input the string!" <<endl;
char ab[20];
gets(ab);
string a(ab);
cout << "Before: " << a <<endl;
rotatestring(a,3);
cout << "After: " << a <<endl;
return 0;
}