-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr.c
More file actions
111 lines (97 loc) · 2.52 KB
/
str.c
File metadata and controls
111 lines (97 loc) · 2.52 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <unistd.h>
#include <fcntl.h>
#include "defs.h"
#include "str.h"
int kstrcmp(char *s1, char *s2) {
while (*s1 == *s2) {
if (*s1 == '\0' && *s2 == '\0') {
return true;
}
s1++;
s2++;
}
return false;
}
void kstrconcat(char *s1, const char *s2) {
// Move pointer to the end of s1
while (*s1 != '\0') {
s1++;
}
// Copy s2 to the end of s1
while (*s2 != '\0') {
*s1++ = *s2++;
}
// Null-terminate the resulting string
*s1 = '\0';
}
int kstrcmp_by_n(char *s1, char *s2, int n) {
int count = 0;
while (count < n && *s1 == *s2) { // Compare up to n characters
if (*s1 == '\0' || *s2 == '\0') { // Stop if either string ends before n characters
break;
}
s1++;
s2++;
count++;
}
// Check if we stopped because we reached n characters or due to a mismatch
return (count == n || (*s1 == *s2));
}
int kstrlen(char *str){
int count = 0;
if(*str == '\0'){
return EXIT_SUCCESS;
}
while(*str != '\0'){
count++;
str++;
}
return count;
}
int kstrcpy(char *src, char *dst) {
if (src == NULL) {
return EXIT_SUCCESS;
}
int c = 0;
while (*src != '\0') {
c++;
*dst++ = *src++;
}
*dst = '\0'; // Ensure the destination string is null-terminated
return c;
}
int kstrhas(char *str, char *substring) {
if (*substring == '\0') {
// If the substring is empty, we assume it is always found
return true;
}
for (int i = 0; str[i] != '\0'; i++) {
int j;
// Check if substring matches at position i
for (j = 0; substring[j] != '\0'; j++) {
if (str[i + j] == '\0' || str[i + j] != substring[j]) {
break; // If characters don't match, break out of the inner loop
}
}
if (substring[j] == '\0') {
// If we completed the inner loop, substring was found
return true;
}
}
// If no match was found after going through the main string
return false;
}
int kstrhas_unary(char *str, char uchar) {
if (kstrlen(str) == 0) {
// If the uchar is empty, we assume it is always found
return true;
}
for (int i = 0; str[i] != '\0'; i++) {
// Check if substring matches at position i
if (str[i] == uchar) {
return true;
}
}
// If no match was found after going through the main string
return false;
}