Skip to content

Commit cde439a

Browse files
committed
"Excel Sheet Column Title"
1 parent b4f4ea7 commit cde439a

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ them with C++ Language.
2626
| 171 | [Excel Sheet Column Number] | |
2727
| 170 | [Two Sum III - Data structure design] | |
2828
| 169 | [Majority Element] | [C](src/169.c) |
29-
| 168 | [Excel Sheet Column Title] | |
29+
| 168 | [Excel Sheet Column Title] | [C](src/168.c) |
3030
| 167 | [Two Sum II - Input array is sorted] | |
3131
| 166 | [Fraction to Recurring Decimal] | |
3232
| 165 | [Compare Version Numbers] | [C](src/165.c) |

src/168.c

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
char *convertToTitle(int n) {
6+
char *ans = (char *)calloc(10, sizeof(char));
7+
8+
int i;
9+
i = 0;
10+
while (n > 0) {
11+
int t = n % 26;
12+
if (t == 0) { t = 26; ans[i] = 'Z'; }
13+
else { ans[i] = t + 'A' - 1; }
14+
n -= t;
15+
n /= 26;
16+
i++;
17+
}
18+
/* reverse string */
19+
int j = strlen(ans) - 1;
20+
i = 0;
21+
while (i < j) {
22+
char t = ans[i];
23+
ans[i] = ans[j];
24+
ans[j] = t;
25+
i++;
26+
j--;
27+
}
28+
29+
return ans;
30+
}
31+
32+
int main() {
33+
int n = 26;
34+
printf("%s\n", convertToTitle(n));
35+
return 0;
36+
}

0 commit comments

Comments
 (0)