-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleftshiftchar.cpp
More file actions
41 lines (40 loc) · 769 Bytes
/
Copy pathleftshiftchar.cpp
File metadata and controls
41 lines (40 loc) · 769 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
/**
* @author: wenlong.cao
* @file: Define a left shift string function. The time compleity should O(n) and
* Space complexity should be O(1)
* @date: 2016-10-31
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
typedef void(*tpf)(char[],const int, int);
void left_shift_onebit(char* s, const int n)
{
char t = s[0];
for(int i = 1; i < n; ++i)
{
s[i - 1] = s[i];
}
s[n-1] = t;
}
void left_shift_method_1(char* s, const int n, int m)
{
while(m--)
{
left_shift_onebit(s, n);
}
}
void test_left_shift(tpf func)
{
char a[] = "abcdfg";
int size = strlen(a);
tpf f = func;
f(a, size, 2);
assert(strcmp(a, "cdfgab") == 0);
}
int main(int argc, char const *argv[])
{
test_left_shift(left_shift_method_1);
return 0;
}