#include<stdio.h>
#include "SeqString.h"
void main()
{
SeqString S1, S2, Sub;
char ch[MAXLEN];
printf("请输入第一个字符串:\n");
gets(ch);
StrAssign(&S1, ch);
printf("输出串S1:");
StrPrint(&S1);
printf("请输入第二个字符串:\n");
gets(ch);
StrAssign(&S2, ch);
printf("输出串S2:");
StrPrint(&S2);
printf("请输入第san个字符串:\n");
gets(ch);
StrAssign(&Sub, ch);
printf("输出串Sub:");
StrPrint(&Sub);
/*
printf("将串S2插入到S1的第13个位置:\n");
StrInsert(&S1, 13, &S2);
StrPrint(&S1);
printf("将S1中的第22个位置起的七个字符删除:\n");
StrDelete(&S1, 22, 7);
StrPrint(&S1);
printf("将串S2中的第6个位置起的4个字符取出来放入Sub中:\n");
SubString(&Sub, &S2, 6, 4);
StrPrint(&Sub);
*/
//printf("将串Sub赋值为America:");
//StrAssign(&Sub, "Amercia");
printf("将串S1中的串S2用Sub取代:\n");
StrReplace(&S1, &S2, &Sub);
StrPrint(&S1);
}
/*SeqString.h*/
#include<stdio.h>
#define MAXLEN 60
typedef struct
{
char str[MAXLEN];
int length;
}SeqString;
void StrAssign(SeqString *S, char cstr[]);//串的赋值操作
int StrEmpty(SeqString *S);//判断串是否为空
int StrLength(SeqString *S);//求串的长度操作
void StrCopy(SeqString *S, SeqString*T);//串的复制操作
int StrCompare(SeqString *S, SeqString *T);//串的比较操作
int StrInsert(SeqString *S, int pos, SeqString *T);//串的插入操作
int StrDelete(SeqString *S, int pos, int len);//串的删除操作
int StrConcat(SeqString *S, SeqString*T);//串的连接操作
int SubString(SeqString *Sub, SeqString *S, int pos, int len);//截取子串操作
int StrReplace(SeqString *S, SeqString *T, SeqString *V);//串的替换操作
int StrIndex(SeqString *S, int pos, SeqString *T);//串的定位操作
void StrClear(SeqString *S);//清空串操作
void StrPrint(SeqString *S);//串的输出声明
/*串的赋值操作*/
void StrAssign(SeqString *S, char cstr[])
{
int i = 0;
for (i = 0; cstr[i] != '\0'; i++)
S->str[i] = cstr[i];
S->length = i;
}
/*判断串是否为空*/
int StrEmpty(SeqString *S)
{
if (S->length == 0)
return 1;
else
return 0;
}
/*求串的长度*/
int StrLength(SeqString *S)
{
return S->length;
}
/*串的复制操作*/
void StrCopy(SeqString *S, SeqString*T)
{
int i;
for (i = 0; i < T->length; i++)
S->str[i] = T->str[i];
S->length = T->length;
}
/*串的比较操作*/
int StrCompare(SeqString *S, SeqString *T)
{
int i;
for (i = 0; i < S->length&&i < T->length;i++)
if (S->str[i] != T->str[i]) //如果出现字符不同,则返回两个字符的差值
return (S->str[i] - T->str[i]);
return (S->length - T->length); //比较完毕,返回两个串的长度的差值
}
/*串的插入操作*/
int StrInsert(SeqString *S, int pos, SeqString *T)
{
int i;
if (pos<0 || pos-1 > S->length)
{
printf("插入的位置不对!\n");
return 0;
}
if (S->length+T->length<=MAXLEN)//第一种情况,子串T完整地插入到串S中
{
/*在插入子串T前,将S中pos后的字符向后移动len个位置*/
for (i = S->length + T->length - 1; i > pos + T->length; i--)
S->str[i] = S->str[i - T->length];
for (i = 0; i < T->length; i++)
S->str[pos + i - 1] = T->str[i];
S->length = S->length + T->length;
return 1;
}
else if (pos+T->length<=MAXLEN)//第二种情况,子串可以完全插入,但S中的字符会被截掉
{
for (i = MAXLEN - 1; i>T->length + pos - 1;i--)
/*将S中pos以后的字符整体移动到数组的最后*/
S->str[i] = S->str[i - T->length];
for (i = 0; i < T->length; i++)
S->str[pos + i - 1] = T->str[i];
S->length = MAXLEN;
return 0;
}
else //第三种情况,子串T不能被完全插入到S中,T中的部分字符会被舍弃
{
for (i = 0; i <MAXLEN-pos; i++)//将T直接插入到S中,插入之前不需要移动S中的字符
S->str[pos + i - 1] = T->str[i];
S->length = MAXLEN;
return 0;
}
}
/*串的删除操作*/
int StrDelete(SeqString *S, int pos, int len)
{
int i;
if (pos<0 || len<0 || pos+len-1 > S->length)
{
printf("删除的位置不正确,参数len不合法!\n");
return 0;
}
else
{
for (i = pos + len -1 ; i <= S->length - 1; i++)
/*将串S的第pos个位置以后的len的字符覆盖掉,从pos位置开始*/
S->str[i - len] = S->str[i];
S->length = S->length - len;
return 1;
}
}
/*串的连接操作*/
int StrConcat(SeqString *S, SeqString*T)
{
int i, flag;
/*第一种情况,将T直接连接到S的末尾*/
if (S->length + T->length <= MAXLEN)
{
for (i = S->length; i < S->length + T->length; i++)
S->str[i] = T->str[i - S->length];
S->length = S->length + T->length;
flag = 1;
}
else if (S->length < MAXLEN)//第二种情况,T的部分字符连接到S末尾
{
for (i = S->length; i<MAXLEN; i++)
S->str[i] = T->str[i - S->length];
S->length = MAXLEN;
flag = 0;
}
return flag;
}
/*截取子串操作*/
int SubString(SeqString *Sub, SeqString *S, int pos, int len)
/*将从串S中的第pos个位置截取长度为len的子串复制给Sub*/
{
int i;
if (pos<0 || len<0 || pos + len - 1>S->length)
{
printf("参数len和pos不合法!\n");
return 0;
}
else
{
for (i = 0; i < len; i++)
Sub->str[i] = S->str[i + pos - 1];
Sub->length = len;
return 1;
}
}
/*串的替换操作*/
int StrReplace(SeqString *S, SeqString *T, SeqString *V)
{
int i=0 ;
int flag;
if (StrEmpty(T))
return 0;
do
{
i = StrIndex(S, i, T);/*利用串的定位操作在串S中查找T的位置*/
if (i)
{
StrDelete(S, i, StrLength(T));/*如果找到子串T,删除S中的子串T*/
flag = StrInsert(S, i, V);/*将子串V插入到原来删除T的位置*/
if (!flag) /*如果没有插入成功,则返回0*/
return 0;
i += StrLength(V);/*在串S中,跳过子串V长度个字符,继续查找T*/
}
} while (i);
return 1;
}
/*串的定位操作*/
int StrIndex(SeqString *S, int pos, SeqString *T)
{
int i, j;
if (StrEmpty(T))
return 0;
i = pos;
j = 0;
while (i < S->length && j < T->length)
{
if (S->str[i]==T->str[j])
/*如果S和T中对应的字符相等,则继续比较下一个字符*/
{
i++;
j++;
}
else
/*如果当前对应的位置的字符不相等,则从串S的下一个字符开始,从T的第0个字符开始比较*/
{
i = i - j + 1;
j = 0;
}
}
if (j >= T->length) /*如果在S中找到串T,则返回子串T在串S中的位置*/
return (i - j + 1);
else
return -1;
}
/*清空串的操作*/
void StrClear(SeqString *S)
{
S->length = 0;
}
/*串的输出*/
void StrPrint(SeqString *S)
{
int i;
for (i = 0; i < S->length; i++)
{
printf("%c", S->str[i]);
}
printf("\n");
}