-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzigzag-conversion.c
More file actions
41 lines (33 loc) · 806 Bytes
/
zigzag-conversion.c
File metadata and controls
41 lines (33 loc) · 806 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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char* convert(char* s, int numRows);
int main() {
char* s;
s = convert("PAYPALISHIRING", 2);
printf("%s\n", s);
return 0;
}
char* convert(char* s, int numRows) {
if(numRows == 1) return s;
int interval = 2 * numRows - 2 ;
int i = 0;
int j;
int length = strlen(s);
char converted[length+1];
int p = 0;
int next_j;
for(i = 0; i < numRows; i++){
for(j = i; j < length; j += interval){
converted[p] = s[j];
p++;
next_j = (j - i) + interval - i;
if(i != 0 && i != numRows - 1 && next_j < length){
converted[p] = s[next_j];
p++;
}
}
}
converted[p] = '\0';
return converted;
}