系统函数C字符串的实现(12):strset

//函数名: strset
//	功  能 : 将一个串中的所有字符都设为指定字符
//	用  法 : char *strset(char *str, char c);
char *mystrset(char *str, char c)
{
	for (char* pnew = str; *pnew != '\0'; *pnew=c,pnew++)
	{
	}
}
//递归方式
void *mystrsetdg(char *str, char c)
{
	if (*str=='\0')
	{
		return NULL;
	}
	else
	{
		*str = c;
		mystrsetdg(++str, c);
	}
}
void main()
{
	char*p = (char[10]){ 0 };
	strcpy(p, "黑夜-zrf");
	printf("p=%s\n", p);

	mystrsetdg(p, 'a');//将字符串所有设置为字符a
	printf("p=%s\n", p);

	mystrset(p, 'o');
	printf("p=%s\n", p);

	mystrsetdg(p, '0');
	printf("p=%s\n", p);

	mystrset(p, '\0');
	printf("p=%s\n", p);
	system("pause");
}

posted on 2017-05-08 12:19  wgwyanfs  阅读(203)  评论(0编辑  收藏  举报

导航