用C语言编写程序1.用StrAssign生成S1=’THIS IS A BOOK’,S2=’S’,S3=’ESE ARE’ 用SubString(S,S1,3,7)求出S=’IS IS A’ 用ConCat(T,S1,S2)求出T=’THIS IS A BOOKS’ 用Replace(T,S,S3)求出T=’THESE ARE BOOKS’(选做) 2. 用StrAssign生成U=’XYXYXYXYXYXY’,W=’w’ 用SubString(V,U,6,3)求出V=’YXY’ 用Replace(U,V,W)求出U=’XWXWXW’ (四) 实现提示 注意参照课本76—77页,对Replace函数的设计要详加考虑。 Status StrAssign(HString *t,char *chars){ /*生成一个其值等于串常量chars的串T*/ int i,j; char *c; if(t->ch) free(t->ch);/*释放T原有空间*/ for(i=0,c=chars;c[i]!='\0';i++);/*求chars的长度*/ if(!i) {t->ch=NULL;t->length=0;} else { t->ch=(char *)malloc(i *sizeof(char)); if(!(t->ch)) exit(OVERFLOW); for(j=0;j<i;j++) t->ch[j]=chars[j]; t->length=i; } return OK; } 其他函数自己修改
时间: 2024-03-16 07:45:54 浏览: 153
好的,以下是用C语言编写的程序:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSTRLEN 255
typedef struct {
char *ch;
int length;
} HString;
int StrAssign(HString *t, char *chars) {
/*生成一个其值等于串常量chars的串T*/
int i, j;
char *c;
if (t->ch) free(t->ch);/*释放T原有空间*/
for (i = 0, c = chars; c[i] != '\0'; i++);/*求chars的长度*/
if (!i) {
t->ch = NULL;
t->length = 0;
} else {
t->ch = (char *) malloc(i * sizeof(char));
if (!(t->ch))
exit(OVERFLOW);
for (j = 0; j < i; j++)
t->ch[j] = chars[j];
t->length = i;
}
return OK;
}
int SubString(HString *sub, HString *s, int pos, int len) {
/*用sub返回串s的第pos个字符起长度为len的子串*/
int i, j;
if (pos < 1 || pos > s->length || len < 0 || len > s->length - pos + 1) {
return ERROR;
}
if (sub->ch) free(sub->ch);/*释放sub原有空间*/
if (!len) {
sub->ch = NULL;
sub->length = 0;
} else {
sub->ch = (char *) malloc(len * sizeof(char));
if (!(sub->ch)) {
exit(OVERFLOW);
}
for (i = pos - 1, j = 0; j < len; i++, j++) {
sub->ch[j] = s->ch[i];
}
sub->length = len;
}
return OK;
}
int ConCat(HString *t, HString *s1, HString *s2) {
/*用t返回由s1和s2连接而成的新串*/
int i, j;
if (t->ch) free(t->ch);/*释放t原有空间*/
t->ch = (char *) malloc((s1->length + s2->length) * sizeof(char));
if (!(t->ch)) {
exit(OVERFLOW);
}
for (i = 0; i < s1->length; i++) {
t->ch[i] = s1->ch[i];
}
for (j = 0; j < s2->length; j++, i++) {
t->ch[i] = s2->ch[j];
}
t->length = s1->length + s2->length;
return OK;
}
int Replace(HString *s, HString *t, HString *v) {
/*用v替换主串s中出现的所有与t相等的不重叠的子串*/
int i, j, k;
if (!t->length) {
return ERROR;
}
i = 1;
while (i <= s->length - t->length + 1) {
SubString(v, s, i, t->length);
if (strcmp(v->ch, t->ch) == 0) {
for (j = i, k = 0; k < v->length; j++, k++) {
s->ch[j - 1] = v->ch[k];
}
s->length = s->length - t->length + v->length;
for (j = i + v->length; j <= s->length; j++) {
s->ch[j - 1] = s->ch[j + t->length - v->length - 1];
}
} else {
i++;
}
}
return OK;
}
int main() {
HString S1, S2, S3, S, T, U, V, W;
// 生成S1、S2、S3
StrAssign(&S1, "THIS IS A BOOK");
StrAssign(&S2, "S");
StrAssign(&S3, "ESE ARE");
// 求出S
SubString(&S, &S1, 3, 7);
// 求出T
ConCat(&T, &S1, &S2);
// 替换T中的S为S3
Replace(&T, &S, &S3);
printf("%s\n", T.ch); // 输出结果:THESE ARE BOOKS
// 生成U、W
StrAssign(&U, "XYXYXYXYXYXY");
StrAssign(&W, "w");
// 求出V
SubString(&V, &U, 6, 3);
// 替换U中的V为W
Replace(&U, &V, &W);
printf("%s\n", U.ch); // 输出结果:XWXWXW
return 0;
}
```
阅读全文