串
#include<stdio.h>
#include<stdlib.h>
#define MAX 250
typedef struct{
char data[MAX];
int length;
} defstring;
//获取字符串长度
int getlen(char str[]){
int i=0;
while(str[i])
i ++;
return i;
}
//初始化
int init_str(defstring *S,char str[]){
int i=0;
int len = getlen(str);
S->length = 0;
while(i<len && S->length<=MAX){
S->data[i] = str[i];
i++;
S->length ++;
}
return 1;
}
//拼接 s1+s2 = T
int str_concat(char s1[],char s2[],defstring *T){
int len_1 = getlen(s1);
int len_2 = getlen(s2);
int i=0;
T->length = 0;
//拼接长度小于MAX的情况
if(len_1 + len_2 <= MAX){
while(i<len_1){
T->data[i] = s1[i];
T->length ++;
i++;
}
i=0;
while(i<len_2){
T->data[T->length] = s2[i];
T->length++;
i++;
}
}else{ //拼接长度大于MAX的情况(两种)
if(len_1 >= MAX){
for(int j=0;j<MAX;j++){
T->data[j] = s1[j];
T->length ++;
}
}else{
i=0;
while(i<len_1){
T->data[i] = s1[i];
T->length ++;
i++;
}
for(int j=0;j<MAX-i;j++){
T->data[T->length] = s2[j];
T->length ++;
}
}
}
return 1;
}
//插入
int ins_str(defstring *S,int address,char ins[]){
int len_ins = getlen(ins);
int j = 0;
int pos = (S->length)-1;
//判断是否可插入
if(address<1 || address>(S->length) || len_ins>(S->length) || len_ins == 0)
return 0;
//字符串向后移动
for(int i=1;i<=S->length-address;i++){
S->data[pos+len_ins] = S->data[pos];
pos --;
}
//字符串插入
while(j<len_ins){
S->data[address+j] = ins[j];
j++;
S->length++;
}
return 1;
}
//删除
int del_str(defstring *D,int address,int len,char del[]){
//判断是否可删除
if(len>(D->length) || len<0 || address<1 || address>(D->length))
return 0;
//存储被删除的串
for(int i=0;i<len;i++){
D->data[address-1+i] = del[i];
D->length --;
}
//字符串向前填补
for(int i=0;i<((D->length)-address+len+1);i++)
D->data[address-1+i] = D->data[address+len-1+i];
return 1;
}
//查找、匹配
int find_str(defstring *F,char find[],int pos){
int i=pos-1;
int j=0;
int len_find = getlen(find);
int len_per = F->length;
//判断是否可查找
if(len_find == 0 || len_find > F->length || pos <1 || pos > F->length)
return -2;
//查找成功返回位置,查找失败返回-1
while(i<=len_per && j<=len_find){
if(F->data[i] == find[j]){
i++;
j++;
if(j == len_find)
return (i-len_find);
}else{
i = i-j+1;
j=0;
}
}
return -1;
}
//打印输出
void display(defstring *S){
printf("数据为:");
for(int i=0;i<S->length;i++){
printf("%3c",S->data[i]);
}
printf("\n");
printf("长度为:%d\n",S->length);
printf("\n");
}
int main(){
defstring S; //测试初始化
defstring T; //测试拼接
char s1[MAX];
char s2[MAX];
defstring I; //测试插入
char ins[MAX];
defstring D; //测试删除
char del[MAX];
defstring F; //测试匹配
char find[MAX];
int pos;
int index;
char str[MAX]; //公共使用变量
int address;
int len;
/*
//初始化测试模块
printf("请输入一个字符串:");
scanf("%s",&str);
init_str(&S,str); //初始化
display(&S);
*/
/*
//拼接测试模块
printf("请输入需要拼接的字符串s1:");
scanf("%s",&s1);
printf("请输入需要拼接的字符串s2:");
scanf("%s",&s2);
str_concat(s1,s2,&T);
display(&T);
*/
/*
//插入测试模块
printf("请输入一个字符串:");
scanf("%s",&str);
init_str(&I,str); //初始化
printf("请输入需要插入的字符串ins:");
scanf("%s",&ins);
printf("请输入插入位置address:");
scanf("%d",&address);
ins_str(&I,address,ins);
display(&I);
*/
/*
//删除测试模块
printf("请输入一个字符串:");
scanf("%s",&str);
init_str(&D,str); //初始化
printf("请输入删除位置address:");
scanf("%d",&address);
printf("请输入删除长度len:");
scanf("%d",&len);
del_str(&D,address,len,del);
display(&D);
*/
/*
//查找/匹配测试模块
printf("请输入一个字符串作为母串:");
scanf("%s",&str);
init_str(&F,str); //初始化
printf("请输入要查找的字符串find:");
scanf("%s",&find);
printf("请输入查找的起始位置pos:");
scanf("%d",&pos);
index = find_str(&F,find,pos);
if(index == -2) printf("ERROR!\n");
else if(index == -1) printf("no this string!\n");
else printf("该子串在第 %d 个位置上\n",index);
*/
return 0;
}