-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.c
More file actions
77 lines (66 loc) · 1.65 KB
/
Copy pathstring.c
File metadata and controls
77 lines (66 loc) · 1.65 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "string.h"
// Calculate length of a string
size_t strlen(const char* str) {
size_t len = 0;
while (str[len] != '\0') {
len++;
}
return len;
}
// Compare two strings
int strcmp(const char* str1, const char* str2) {
while (*str1 && (*str1 == *str2)) {
str1++;
str2++;
}
return *(unsigned char*)str1 - *(unsigned char*)str2;
}
// Fill memory with a specific byte
void* memset(void* dest, int val, size_t len) {
unsigned char* ptr = (unsigned char*)dest;
while (len-- > 0) {
*ptr++ = (unsigned char)val;
}
return dest;
}
// Helper to reverse a string (used in itoa)
void reverse(char* str, int length) {
int start = 0;
int end = length - 1;
while (start < end) {
char temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}
// Convert integer to ASCII string
void itoa(int num, char* str, int base) {
int i = 0;
int isNegative = 0;
// Handle 0 explicitly, otherwise empty string is returned
if (num == 0) {
str[i++] = '0';
str[i] = '\0';
return;
}
// In standard itoa(), negative numbers are handled only with base 10.
if (num < 0 && base == 10) {
isNegative = 1;
num = -num;
}
// Process individual digits
while (num != 0) {
int rem = num % base;
str[i++] = (rem > 9) ? (rem - 10) + 'a' : rem + '0';
num = num / base;
}
// If number is negative, append '-'
if (isNegative) {
str[i++] = '-';
}
str[i] = '\0'; // Append string terminator
// Reverse the string
reverse(str, i);
}