首页 > 代码库 > C和指针 (pointers on C)——第六章:指针(下)习题

C和指针 (pointers on C)——第六章:指针(下)习题

有一定的难度,作者只做了前三题!

题目请见 http://download.csdn.net/download/wangpegasus/5701765

1、

char *find_char(char const *source, char const *chars)
{
	char *ptr;
	if(source != NULL && chars != NULL)
	{
		for (; *source != '\0'; source++)
		{
			for (ptr = (char *) chars; *ptr != '\0'; ptr++)
			{
				if (*ptr = * source)
				{
					return (char *)source;
				}
			}
		}
	}
	return NULL;
}
2、

char *match(char *string, char *sub)
{
	while (*sub != '\0')
	{
		string++;
		sub++;
		if (*string != *sub)
		{
			return NULL;
		}
	}
	return string;
}
int del_substr( char * str, char const  *substr)
{
	char * ptr;
	ptr = NULL;
	if ( str == NULL && substr == NULL)
	{
		return 0;
	}
	for (; *str != '\0'; str++)
	{
		ptr = match(str, (char *)substr);
		if (ptr != NULL)
		{
			break;
		}
	}
	while (*str++ = *ptr++)
	{
		;
	}
	return 1;
}
3、

void reverse_string(char *string)
{
	char temp;
	char * last;
	last = string;
	if (string != NULL)
	{
		while (*last != '\0')
		{
			last++;
		}
		last--;
		while (string > last)
		{
			temp = *last;
			*last = *string;
			*string = temp;
			string--;
			last++;
		}
	}
}