字符串(简称串)
是一种特殊的线性表,它的每个结点仅由一个字符组成。
(也可以称–有0个或多个字符组成的有限序列叫做串)
记作: S = “a1a2a3…an”;(n>=0)
- 其中S是串名
- 用双引号 “” 括起来的字符序列是串的值
- 串字符的数目n称为该串的长度
- 注:长度为零(n=0)的串称为空串它不包含任何字符,记作“”
- 空串“”和空格串“ ”是有区别的
- 例如:s1 = “”;s2=" ". s1中没有字符,是一个空串;而s2中有两个空格字符,它的长度等于2,它是由空格字符组成的串,一般称此为空格串,它的长度为串中空格的个数
- 主串与子串
- 串中任意个连续字符组成的子序列称为该串的子串
- 包含子串的串称为该子串的主串
- 串的比较
- 两个串之间可以进行比较
- 两串相等条件
- 长度相同
- 对应位置的字符相同
- 若两串不相同时,可按ASCII码大小进行比较
- ‘0’ --48
- ‘A’ --65
- ‘a’ --97
串的基本操作
- 结构
struct String {
char string[maxSize];
int length;
};
- 初始化
struct String * initString() {
struct String *s = (struct String *)malloc(sizeof(struct String));
s->length = 0;
return s;
}
- 求串长
int length(struct String *s) {
return s->length;
}
- 判断是否为空
int isNull(struct String *s) {
if (s->length == 0) {
return 1;
}
else {
return 0;
}
}
- 输入数据
int input(struct String *s) {
printf("Input data :");
char data;
fflush(stdin);
while ((data = getchar())!='\n') {
if (s->length>= maxSize) {
fflush(stdin);
data = NULL;
printf("--->>>Input error(Byoned length)\n");
return;
}else {
s->string[s->length] = data;
s->length++;
}
}
}
- 输出数据
void output(struct String *s) {
printf("Data:");
for (int i = 0; i < s->length;i++) {
printf("%c",s->string[i]);
}
printf("\n");
}
- 拷贝字符串
struct String * strCopy(struct String *s,struct String *t) {
for (int i = 0; i < s->length;i++) {
t->string[i] = s->string[i];
}
t->length = s->length;
return t;
}
- 字符串连接
struct String * strCat(struct String *s,struct String *t) {
for (int i = 0; i < t->length;i++) {
if (s->length >= maxSize) {
printf("--->>>Input error(Byoned length)\n");
break;
}
else {
s->string[s->length] = t->string[i];
s->length++;
}
}
return s;
}
- 截取字符
struct String* subStr(struct String* s, int loc, int length) {
struct String* result = initString();
if (loc<0 || length>s->length) {
printf("--->>> location Error\n");
return;
}
else {
for (int i = loc; i <= length; i++) {
result->string[result->length] = s->string[i];
result->length++;
}
}
return result;
}
- 删除字符
void delStr(struct String* s, int loc) {
if (loc >= 0 && loc<s->length) {
for (int i = loc; i < s->length; i++) {
s->string[i] = s->string[i + 1];
}
}
else {
printf("--->>> Location error\n");
return;
}
}
- 插入字符
struct String * insertStr(struct String *s,int loc,char data) {
if (loc<0 || loc>=s->length) {
printf("--->>> Location error");
return;
}
for (int i = s->length - 1; i >= loc;i--) {
s->string[i + 1] = s->string[i];
}
s->string[loc] = data;
s->length++;
}
- 替换字符
void replaceStr(struct String *s,char math,char data) {
for (int i = 0; i < s->length;i++) {
if (s->string[i] == math) {
s->string[i] = data;
}
}
}
- main函数测试
void main() {
struct String *s = initString();
if (s) {
printf("--->>> Init succuess!!! length : %d\n",s->length);
}
if (isNull(s)) {
printf("--->>> String is null\n");
}else {
printf("--->>>String is not null\n");
}
//测试
struct String* t = initString();
input(s);
//strCopy(s,t);
//input(t);
//t = strCat(s,t);
//t = subStr(s,0,25);
//output(t);
//delStr(s,2);
insertStr(s,1,'X');
//replaceStr(s,'a','X');
output(s);
}