-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsstring.c
More file actions
143 lines (128 loc) · 3.73 KB
/
sstring.c
File metadata and controls
143 lines (128 loc) · 3.73 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
WeChat: cstutorcs
QQ: 749389476
Email: tutorcs@163.com
/**
* vector
* CS 241
*/
#include "sstring.h"
#include "vector.h"
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <assert.h>
#include <string.h>
struct sstring {
// Anything you want
char* string;
size_t length;
};
sstring *cstr_to_sstring(const char *input) {
// your code goes here
sstring* s = malloc(sizeof(sstring));
s->string = calloc(strlen(input) + 1, sizeof(char));
s->length = strlen(input);
strcpy(s->string, input);
return s;
}
char *sstring_to_cstr(sstring *input) {
// your code goes here
char* res = calloc(input->length + 1, sizeof(char));
strcpy(res, input->string);
return res;
}
int sstring_append(sstring *this, sstring *addition) {
// your code goes here
this->length += addition->length;
this->string = realloc(this->string, this->length + 1);
strcat(this->string, addition->string);
return this->length;
}
vector *sstring_split(sstring *this, char delimiter) {
// your code goes here
vector* res = string_vector_create();
/*
char* cur = strtok(this->string, &delimiter);
while (cur) {
vector_push_back(res, cur);
cur = strtok(NULL, &delimiter);
}
*/
size_t l = 0;
size_t r = 0;
while (l < this->length) {
while (r < this->length && this->string[r] != delimiter) r++;
char* cur = calloc(r - l + 1, 1);
size_t i = 0;
for (; i < r - l; i++) {
cur[i] = this->string[l + i];
}
vector_push_back(res, cur);
free(cur);
if (r == this->length - 1) {
char* last = calloc(1, 1);
vector_push_back(res, last);
free(last);
}
r++;
l = r;
}
return res;
}
int str_compare(char* str, char *target) {
for (size_t i = 0; i < strlen(target); i++) {
if (str[i] == 0) return -1;
if (str[i] != target[i]) return 0;
}
return 1;
}
int sstring_substitute(sstring *this, size_t offset, char *target,
char *substitution) {
// your code goes here
size_t i = offset;
for (; i < this->length; i++) {
int flag = str_compare(this->string + i, target);
if (flag == 1) {
size_t t_len = strlen(target);
size_t s_len = strlen(substitution);
size_t j = 0;
if (t_len < s_len) {
//target less than the substitution
this->length = this->length - t_len + s_len;
this->string = realloc(this->string, this->length + 1);
for (j = this->length; j >= i + s_len; j--) {
this->string[j] = this->string[j - s_len + t_len];
}
} else if (t_len > s_len) {
//target longer than the substitution
this->length = this->length - t_len + s_len;
for (j = i + s_len; j <= this->length; j++) {
this->string[j] = this->string[j - s_len + t_len];
}
this->string = realloc(this->string, this->length + 1);
}
//substitute the substring
for (j = 0; j < s_len; j++) {
this->string[i + j] = substitution[j];
}
return 0;
}
if (flag == -1) return -1;
}
return -1;
}
char *sstring_slice(sstring *this, int start, int end) {
// your code goes here
char* res = calloc(end - start + 1, 1);
int i = 0;
for (; i < end - start; i++) {
res[i] = this->string[start + i];
}
return res;
}
void sstring_destroy(sstring *this) {
// your code goes here
free(this->string);
free(this);
this = NULL;
}