-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
64 lines (59 loc) · 980 Bytes
/
util.c
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <stdio.h>
#include <stdlib.h>
#include "util.h"
void *reverse(char *s, int i, int j)
{
char temp;
char *p = s + i;
char *q = s + j;
while (p < q) {
temp = *p;
*p = *q;
*q = temp;
p++; q--;
}
}
char *itos(int i)
{
char *s = (char *)malloc(sizeof(char) * MAX_STR_LEN);
int j = 0;
if (i == 0) {
s[0] = '0';
s[1] = '\0';
return s;
}
while (i != 0)
{
s[j++] = i % 10 + '0';
i /= 10;
}
s[j] = '\0';
return s;
}
int stoi(char *s)
{
int ans = 0;
int len = strle(s);
//printf("%d\n", len);
for (int i = 0; i < len; i++) {
ans = ans * 10 + s[i] - '0';
}
return ans;
}
int strle(char *s)
{
int i = 0;
while(*s++ != '\0') {
i++;
}
return i;
}
int strcmp(char *p, char *q)
{
char *a = p;
char *b = q;
while (!(*a - *b) && *b != '\0') {
a++; b++;
}
return *a - *b;
}