85.按要求替换字符串中的内容

函数fun的功能是:将s所指字符串中最后一次出现的与t1所指字符串相同的zi串替换成她所指字符串,所形成的新串放在w所指的数组中。


#define _CRT_SECURE_NO_WARNINGS
#include
#include
void fun(char *s, char *t1, char *t2, char *w)
{
	int i = 0,n=0;
	char *p, *r, *a=NULL,t[100];
	strcpy(w, s);
	while (*w)
	{
		p = w;
		r = t1;
		while ((*r == *p)&&(*r!=0))
		{
			r++;
			p++;
		}
		if (*r == '\0')
			a = w;
		w++;
	}
	n = strlen(t1);
	p = a;
	while (*(p + n))
	{
		t[i] = *(p + n);
		i++;
		p++;
	}
	t[i] = 0;
	i = 0;
	r = t2;
	while (*r)
	{
		*a = *r;
		a++;
		r++;
	}
	while (t[i])
	{
		*a = t[i];
		a++;
		i++;
	}
	*a = 0;
}
int main()
{
	char s[100], t1[100], t2[100], w[100];
	printf("\nPlease enter string S:");
	scanf("%s", s);
	printf("\nPlease enter substring t1:");
	scanf("%s", t1);
	printf("\nPlease enter substring t2:");
	scanf("%s", t2);
	fun(s, t1, t2, w);
	printf("\nThe result is: %s\n", w);
	system("pause");
	return 0;
}


你可能感兴趣的:(c习题)