#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 60
typedef struct
{
char *ch;
int length;
} HString;
void initstr(HString *T) //初始化,产生空串。
{
//T->ch = (char*)malloc(sizeof(char)*MAXSIZE); // 听某本书上说,永远都不用写sizeof(char),sizeof(char)恒为1
T->ch = malloc(MAXSIZE);
if(!(T->ch))
printf("对不起,初始化失败。\n");
else
{
T->length = 0;
printf("恭喜你,初始化成功。\n");
}
}
void creatstr(HString *T) //初始化开辟空间。
{
//char s[MAXSIZE];
//int len, i;
printf("请输入您想要的字符串:");
//gets(s);
//len = strlen(s);
//for(i = 0; i<len; i++)
//{
// T->ch[i] = s[i];
//}
//T->ch[i] = '\0';
//T->length = len;
// 为什么要用gets,把字符串get到s,然后再复制到T->ch ? ^_^
fgets(T->ch, MAXSIZE, stdin);
T->length = strlen(T->ch) - 1; // '\n'
T->ch[T->length] = '\0';
printf("字符串赋值成功!\n");
}
void printstr(HString *T)
{
printf("%s\n", T->ch);
printf("打印成功!\n");
}
void concatstr(HString *T1, HString *T2) //字符串连接
{
int lengthstr(HString *T);
int i, j, len1, len2;
len1 = lengthstr(T1);
len2 = lengthstr(T2);
for(i = len1, j = 0; i <= len1 + len2; i++)
{
T1->ch[i] = T2->ch[j];
j++;
}
T1->length = len1 + len2;
}
int lengthstr(HString *T) //求字符串的长度
{
int len, i;
len = 0;
i = 0;
while(T->ch[i] != '\0')
{
len++;
i++;
}
printf("您的字符串长度为:%d\n", len);
return len;
}
void deletesubstr(HString *T, int pos, int len) //删除从pos位置起的len个字符 注意应加上判断删除位置等是否正确,此处省略,有时间请补上。
{
int i;
for(i = pos + len - 1; i < T->length; i++)
{
T->ch[i - len] = T->ch[i];
}
T->length = T->length - len;
T->ch[T->length] = '\0';
}
void insertsubstr(HString *T, int pos, HString S) //在pos位置之前插入一个字符串S;
{
int i, len, j;
len = S.length;
for(i = T->length - 1; i >= pos - 1; i--) //该循环用以使pos字符(包含pos字符)之后的每一个字符后移,为子串腾出位置。
{
T->ch[i + len] = T->ch[i];
}
T->length = T->length + len;
T->ch[T->length] = '\0';
i = pos - 1;
for(i, j = 0; j<len; j++, i++) //该循环用以将腾出的位置插入子串
{
T->ch[i] = S.ch[j];
}
}
int Indexstr(HString *T, int pos, HString S)//从第pos个字符开始寻找子串S,若找到,则返回其位置。
{
int i, j;
i = pos - 1;
j = 0;
while(i <= T->length - 1 && j <= S.length - 1)
{
if(T->ch[i] == S.ch[j])
{
++i;
++j;
}
else
{
i = i - j + 1;
j = 0;
}
}
if(j >= S.length)
{
printf("您所需要查询的子串在第%d个位置\n", i - S.length + 1);
return i - S.length + 1;
}
else
{
printf("尚未查询到您需要的子串\n");
return 0;
}
}
void replacestr(HString *T, HString T1, HString T2)//将字符串T中等于T1的子串替换成为T2
{
int Indexstr(HString *, int, HString);
void insertsubstr(HString *, int, HString);
void deletesubstr(HString *, int, int);
int pos;
pos = 0;
pos = Indexstr(T, 1, T1);
while(pos)
{
deletesubstr(T, pos, T1.length);
insertsubstr(T, pos, T2);
pos = Indexstr(T, pos, T1);
}
printf("替换完毕.\n");
}
int main()
{
HString T1, T2, T3;
char str[] = "hsy";
char str2[] = "cute";
initstr(&T1);
creatstr(&T1);
T2.ch = str;
T3.ch = str2;
T2.length = strlen(T2.ch);//T2.length=lengthstr(&T2);
T3.length = strlen(T3.ch);
//initstr(&T2);
//creatstr(&T2);
//printstr(&T);
//concatstr(&T1,&T2);
//lengthstr(&T1);
//printstr(&T1);
//deletesubstr(&T1,4,2);
//insertsubstr(&T1,3,T2);
//printstr(&T1);
//Indexstr(&T1,1,T2);
printstr(&T1);
replacestr(&T1, T2, T3);
printstr(&T1);
return 0;
}