-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzigzag_conversion.c
More file actions
36 lines (30 loc) · 993 Bytes
/
Copy pathzigzag_conversion.c
File metadata and controls
36 lines (30 loc) · 993 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
char * convert(char * s, int numRows){
unsigned int len = strlen(s);
char* result = calloc(len + 1, sizeof(char));
int write_i = 0;
int row_to_write = 0;
while(write_i < len) {
// Each loop the read index progresses through the loop from 0.
// A row index zigzags back an forth between 0 and numRows - 1
// while the read index moves forward, mapping each index in the
// original string to a row in a zigzag pattern.
int read_i = 0;
int row = 0;
int row_vel = -1;
while(read_i < len) {
if(row == row_to_write) {
result[write_i++] = s[read_i];
}
if(row == 0) {
row_vel = 1;
}
else if(row == (numRows - 1)) {
row_vel = -1;
}
row += row_vel;
read_i++;
}
++row_to_write;
}
return result;
}