Skip to content

Commit d94c441

Browse files
committed
Add:
char *ToUpper(const char *str); char *ToLower(const char *str); int Count(const char *s, const char *sep);
1 parent 211af65 commit d94c441

2 files changed

Lines changed: 97 additions & 5 deletions

File tree

‎c/String/README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
- [ ] func Replace(str string, old string, new string, n int)
1818
- [ ] func Title(s string) string
1919
- [ ] func ToTitle(s string) string
20-
- [ ] func ToLower(str string) string
21-
- [ ] func ToUpper(str string) string
20+
- [x] func ToLower(str string) string
21+
- [x] func ToUpper(str string) string
2222
- [ ] func Contains(s, substr string) bool
2323
- [ ] func ContainsAny(s,chars string)bool
24-
- [ ] func Count(s, sep string) int
24+
- [x] func Count(s, sep string) int
2525
- [ ] func Repeat(str string, count int)string
2626
- [ ] func Trim(str string, cut string)
2727
- [ ] func TrimLeft(str string, cut string)

‎c/String/gostring.c‎

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <stdio.h>
2+
#include <stdlib.h>
23
#include <string.h>
3-
// Only English supported; utf8 still have bugs
4+
5+
// Only ASCII supported; bugs still exist on UTF-8
46
_Bool EqualFold(const char *s1, const char *s2)
57
{
68
int i = strlen(s1);
@@ -92,9 +94,99 @@ _Bool HasPrefix(const char *s, const char *prefix)
9294
return 1;
9395
}
9496
}
97+
char *ToUpper(const char *str)
98+
{
99+
char *ret = (char *)malloc(sizeof(char) * (strlen(str) + 1));
100+
int i;
101+
if (ret == NULL)
102+
{
103+
return NULL;
104+
}
105+
else
106+
{
107+
for (i = 0; str[i] != '\0'; i++)
108+
{
109+
if (str[i] >= 'a' && str[i] <= 'z')
110+
{
111+
ret[i] = str[i] - 'a' + 'A';
112+
}
113+
else
114+
{
115+
ret[i] = str[i];
116+
}
117+
}
118+
ret[i] = '\0';
119+
return ret;
120+
}
121+
}
122+
123+
void testToUpper()
124+
{
125+
char array[] = "你好Go!\nHello,GO!";
126+
char *tmp = ToUpper(array);
127+
printf("ToUpper(%s)\n%s", array, tmp);
128+
free(tmp);
129+
}
130+
char *ToLower(const char *str)
131+
{
132+
char *ret = (char *)malloc(sizeof(char) * (strlen(str) + 1));
133+
int i;
134+
if (ret == NULL)
135+
{
136+
return NULL;
137+
}
138+
else
139+
{
140+
for (i = 0; str[i] != '\0'; i++)
141+
{
142+
if (str[i] >= 'A' && str[i] <= 'Z')
143+
{
144+
ret[i] = str[i] - 'A' + 'a';
145+
}
146+
else
147+
{
148+
ret[i] = str[i];
149+
}
150+
}
151+
ret[i] = '\0';
152+
return ret;
153+
}
154+
}
155+
void testToLower()
156+
{
157+
char array[] = "你好Go!\nHello,GO!";
158+
char *tmp = ToLower(array);
159+
printf("ToLower(%s)\n%s", array, tmp);
160+
free(tmp);
161+
}
162+
int Count(const char *s, const char *sep)
163+
{
164+
if (sep[0] == '\0')
165+
{
166+
return strlen(s) + 1;
167+
}
168+
int count = 0;
169+
while ((s = (strstr(s, sep))) != NULL)
170+
{
171+
s++;
172+
count++;
173+
}
174+
return count;
175+
}
176+
void testCount()
177+
{
178+
char *s1 = "1145141919810";
179+
char *s2 = "2";
180+
char *s3 = "";
181+
printf("Count(\"%s\",\"%s\")=%d\n", s1, s2, Count(s1, s2));
182+
printf("Count(\"%s\",\"%s\")=%d\n", s1, s3, Count(s1, s3));
183+
}
95184
int main()
96185
{
97186
// testEqualFold();
98187
// testIndexByte();
99-
testIndex();
188+
// testIndex();
189+
// testToUpper();
190+
// testToLower();
191+
testCount();
100192
}

0 commit comments

Comments
 (0)