字符串处理的一些题

1 写一个函数removesubstr(char *s, char *t),功能是从字符串s中找子串t,并将t从s中删除。

代码:

int removesubstr(char *s, char *t)
{
	char *dst, *sp, *tp;
	int find = 0;
	dst = s;
	while (*s)
	{
		sp = s;
		tp = t;
		while (*tp && (*sp == *tp))
		{
			sp++;
			tp++;
		}
		if (!*tp)
		{
			find = 1;
			break;
		}
		s++;
	}
	if (find == 1)
	{
		while ((*s++ = *sp++) != '\0');
	}
	s = dst;
	return find;
}


2 memcpy与memmove的实现

代码:

void * my_memcpy(void* dest, void* source, size_t count)
{
	char *ret = (char *)dest;
	char *dest_t = ret;
	char *source_t = (char *)source;

	while (count--){
	   *dest_t++ = *source_t++;
	}   
	return ret;
}      


void * my_memmove(void *dst,const void *src,int count)
{
	char *ret;
	char *dst_t;
	char *src_t;

	ret = (char *)dst;

	if ((unsigned char*)dst <= (unsigned char*)src 
		|| (unsigned char *)dst >= ((unsigned char *)src + count)) 
	{
		dst_t = (char *)dst;
		src_t = (char *)src;
		while (count--) 
		{
			*dst_t++ = *src_t++;
		}
	}
	else
	{ 
		dst_t = (char *)dst + count - 1;
		src_t = (char *)src + count - 1;
		while (count--) 
		{
			*dst_t-- = *src_t--;
		}
	}
	return ret;
}




阅读更多
文章标签: dst
个人分类: c/c++
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭