Files
Data-Structure/CLion/ExerciseBook/04.11/String.c
2019-11-12 23:47:30 +08:00

152 lines
3.2 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*===========================
* 习题4.10~4.14中使用的字符串
============================*/
#include "String.h"
// 初始化构造一个值为s的串t
void StrAssign(StringType* t, const char* s) {
if(s == NULL) {
*t = "";
}
*t = (char*) malloc((strlen(s) + 1) * sizeof(char));
strcpy(*t, s);
}
// 比较返回s与t的大小如果大小一致返回0
int StrCompare(StringType s, StringType t) {
return strcmp(s, t);
}
// 计数返回字符串s的长度
int StrLength(StringType s) {
return (int) strlen(s);
}
// 联接返回由s与t联接后的串
StringType Concat(StringType s, StringType t) {
StringType r;
if(s == NULL) {
s = "";
}
if(t == NULL) {
t = "";
}
r = (char*) malloc((strlen(s) + strlen(t) + 1) * sizeof(char));
r[0] = '\0';
strcat(r, s);
strcat(r, t);
return r;
}
// 求子串从s的start位置起截取len个字符后返回
StringType SubString(StringType s, int start, int len) {
int n;
char* sub;
n = (int) strlen(s);
if(start < 1 || start > n || start + len - 1 > n || len < 0) {
return "";
}
sub = (char*) malloc((len + 1) * sizeof(char));
strncpy(sub, s + start - 1, len);
sub[len] = '\0';
return sub;
}
// 查找从s的pos位置起查找t如果找到返回其位置
int Index(StringType s, StringType t, int pos) {
int m, n, i;
StringType sub;
// 失败情形提前处理
if(pos < 1 || pos > StrLength(s) || StrLength(t) == 0) {
return 0;
}
n = StrLength(s);
m = StrLength(t);
i = pos;
// 保证长度不越界
while(i <= n - m + 1) {
// 获取S[i, i+m-1]
sub = SubString(s, i, m);
// 如果子串与模式串不匹配,则需要继续推进
if(StrCompare(sub, t) != 0) {
++i;
} else {
return i;
}
}
return 0;
}
// 插入在s的pos处插入t
Status StrInsert(StringType* s, int pos, StringType t) {
StringType r;
StringType s1, s2;
if(pos < 1 || pos > StrLength(*s) + 1) {
return ERROR;
}
r = (StringType) malloc((strlen(*s) + strlen(t) + 1) * sizeof(char));
r[0] = '\0';
// 如果待插入的串为空,则提前返回
if(StrLength(t) == 0) {
return OK;
}
s1 = SubString(*s, 1, pos - 1);
s2 = SubString(*s, pos, (int) strlen(*s) - pos + 1);
strcat(r, s1);
strcat(r, t);
strcat(r, s2);
*s = r;
return OK;
}
// 删除从s的pos位置起删除len个字符
Status StrDelete(StringType* s, int pos, int len) {
StringType r;
if(pos < 1 || pos + len - 1 > StrLength(*s) || len < 0) {
return ERROR;
}
// 如果待删除的长度为0则提前返回
if(len == 0) {
return OK;
}
r = (StringType) malloc((StrLength(*s) - len + 1) * sizeof(char));
strncpy(r, *s, pos - 1);
strcpy(r + pos - 1, *s + pos + len - 1);
*s = r;
return OK;
}
// 输出字符串
void StrPrint(StringType s) {
printf("%s\n", s);
}