Head.H
#define MAXSIZE 20
typedef struct _str
{
char data[MAXSIZE];
int len;
}Str, *str;
str InitString(void);
void PrintStr(str s);
void StrAssign(str s, char ch[]);
void StrCopy(str a, str b);
int IsStrEqual(str a, str b);
str StrConcat(str a, str b);
void ClearSring(str s);
void InsertString(str s, int pos, str t);
int FindStringIndex(str s, str t);
void SearchandReplaceString(str s, str a, str b);
Function.C
str InitString(void)
{
str s = (str)malloc(sizeof(Str));
return s;
}
void PrintStr(str s)
{
if (s->len == 0)
{
printf("This String is empty ! \n");
return;
}
for (int i = 0; i < s->len; i++)
{
printf("%c ", s->data[i]);
}
printf("\n");
}
void StrAssign(str s, char ch[])
{
int i;
for (i = 0; ch[i] != '\0'; i++)
{
if (i < MAXSIZE - 1)
{
s->data[i] = ch[i];
}
}
s->len = i;
}
void StrCopy(str a, str b)
{
int i;
for (i = 0; i < b->len; i++)
{
a->data[i] = b->data[i];
}
a->len = b->len;
}
int IsStrEqual(str a, str b)
{
int result = 1;
if (a->len != b->len)
{
result = 0;
return result;
}
int i;
for (i = 0; i < a->len; i++)
{
if (a->data[i] != b->data[i])
{
result = 0;
return result;
}
}
return result;
}
str StrConcat(str a, str b)
{
str tempstr = (str)malloc(sizeof(Str));
int i;
for (i = 0; i < a->len; i++)
{
tempstr->data[i] = a->data[i];
}
for (i = 0; i < b->len; i++)
{
tempstr->data[a->len + i] = b->data[i];
}
tempstr->len = a->len + b->len;
return tempstr;
}
void ClearSring(str s)
{
s->len = 0;
}
//在pos的前方插入,pos从0开始
void InsertString(str s, int pos, str t)
{
for (int i = s->len - 1; i >= pos; i--)
{
s->data[i + t->len] = s->data[i];
}
for (int i = 0; i < t->len; i++)
{
s->data[i + pos] = t->data[i];
}
s->len += t->len;
}
//返回index从0开始
int FindStringIndex(str s, str t)
{
if (t->len > s->len)
{
printf("The string be found should shorter ! \n");
return -1;
}
int i = 0;
int matchlen = 0;
while ((i < s->len) && (s->len - i >= t->len))
{
if (s->data[i] == t->data[0])
{
for (int j = 0; j < t->len; j++)
{
if (s->data[i + j] != t->data[j])
{
i++;
matchlen = 0;
break;
}
else
{
matchlen++;
}
if (matchlen == t->len)
{
return i;
}
}
}
else
{
i++;
}
}
return -1;
}
void SearchandReplaceString(str s, str a, str b)
{
if ((a->len == b->len) && (s->len >= a->len))
{
int location = FindStringIndex(s, a);
if (location != -1)
{
for (int i = 0; i < b->len; i++)
{
s->data[location + i] = b->data[i];
}
}
else
{
printf("Cannot find same string in s! \n");
}
}
else
{
printf("error occur ! \n");
return;
}
}
Main.C
void main(void)
{
char a[] = "asd";
char b[] = "bc";
char c[] = "bcd";
char d[] = "acdndkn";
char e[] = "nd";
str s1 = InitString();
str s2 = InitString();
str s3 = InitString();
StrAssign(s1, a);
StrAssign(s2, b);
StrAssign(s3, c); //s1=asd s2=bc s3=bcd
PrintStr(s1);
StrCopy(s1, s2); //s1=bc s2=bc s3=bcd
PrintStr(s1);
PrintStr(s2);
printf("\n");
printf("%d", IsStrEqual(s1, s2)); //1
printf("%d", IsStrEqual(s1, s3)); //0
printf("\n");
str s4 = InitString();
s4 = StrConcat(s1, s3); //s1=bc s2=bc s3=bcd s4=bcbcd
PrintStr(s4);
printf("\n");
ClearSring(s4); //s1=bc s2=bc s3=bcd s4=
PrintStr(s4);
printf("\n");
InsertString(s1, 1, s2); //s1=bbcc s2=bc s3=bcd s4=
PrintStr(s1);
printf("\n");
StrAssign(s4, d); //s1=bbcc s2=bc s3=bcd s4=acdndkn
int location1 = FindStringIndex(s1, s2); //1
int location2 = FindStringIndex(s4, s2); //-1
printf("%d", location1);
printf("%d", location2);
printf("\n");
str s5 = InitString();
StrAssign(s5, e); //s1=bbcc s2=bc s3=bcd s4=acdndkn s5=nd
SearchandReplaceString(s4, s5, s2); //s1=bbcc s2=bc s3=bcd s4=acdbckn s5=nd
PrintStr(s4);
printf("\n");
system("pause");
}