编写一个函数,删除一个字符串中的子串

/**/
char source_string[] = "working hard and to be a diligent people!";
char sub_string[]	= "to be a diligent"; 



unsigned int cal_length(char *str)
{
	unsigned int length = 0;

	for(;*str != '\0';str++)
	{
		length++;
	}

	return length;
}

int del_substr(char *str, char const *substr)
{
	char *p_str;
	char *p_substr;
	char *p_temp;

	p_str		= str;
	p_substr	= substr;
	
	assert( (p_str != NULL)&&(p_substr != NULL));//判断表达式的值,如果source为NULL,就打印出错消息	

	printf("The origine str is:%s\n",p_str);
	printf("The sub str is:%s\n",p_substr); 

	while(*p_str != *p_substr)
	{
		p_str++;
	}
	
	p_temp = p_str + cal_length(p_substr);

	while(*p_substr++ != '\0')
	{
		if(*p_temp != '\0')
		{
		*p_str++ = *p_temp++; 
		}
		else
		{
			*p_str = '\0';
		}
	}
	printf("The origine str changed is:%s\n",str);
	

	return 0;
}


int main(void)
{
	del_substr(source_string,sub_string);
	getch();
	return 0;
}


/**/

猜你喜欢

转载自blog.csdn.net/deep_l_zh/article/details/54949611
今日推荐