作业题3.txt

来自「中南的数据结构试验1」· 文本 代码 · 共 81 行

TXT
81
字号

#define maxlen 250
#include<stdio.h>
#include<stdlib.h>
struct chuan{
int data;
char len[maxlen];
};
struct chuan creats(void)
{       struct chuan l;
	int i;
	for(i=0;i<maxlen;i++)
	{                                        /* 输入主串*/
		scanf("%c",&l.len[i]);
		if(l.len[i]=='\n')
		goto js;
	}
js:    printf("\n*********************\n");
	l.data=i;
	return l;
}
void disp(struct chuan l)
{
	int i;
	printf("\n+++++++++++++++++++\n");
	for(i=0;i<l.data;i++)
	printf("%c",l.len[i]);       /*在屏幕上显示主串*/
	printf("\n");
}
struct chuan replace (struct chuan l1,struct chuan l2,struct chuan l3)
{
	struct chuan l4;
	int i,j,k,m=0,n=0;
	k=l1.data;
	for(i=0;i<k;i++)
	{
		for(j=0;j<l2.data&&m==0;j++)
		{
		if(l1.len[i+j]!=l2.len[j])
		m=1;
		}
	if(m==0){
		for(j=0;j<l3.data;j++)
		{
		l4.len[n]=l3.len[j];
		n++;
		}
		i=i+l2.data-1;
		}
	else   {
		l4.len[n]=l1.len[i];
		n++;
		}
		m=0;
	}
	l4.data=n;
	return l4;
}
main()
{
	int i;
	struct chuan l1,l2,l3,l4;
	clrscr();
	printf("\n it is a replace string!\n");
	printf("***********************\n");
	printf("Enter the host string:\n");    /* 输入主串*/
	l1=creats();
	printf("\nEnter the replace string:\n");   /*输入要被替换的子串*/
	l2=creats();
	printf("\Enter the new string:\n");     /*输入替换的新子串*/
	l3=creats();
	printf("\n**********************\n");
	l4=replace(l1,l2,l3);
	printf("the old string:\n");
	disp(l1);
	printf("\nthe new string:\n");
	disp(l4);
	printf("\n press any key out!");
	getch();
	clrscr();
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?