1. 程式人生 > >【c語言】實現strchr

【c語言】實現strchr

// 實現strchr(在一個字串中查詢字元,找到的話返回指向該字元的指標,沒找到的話返回空)

#include <stdio.h>
#include <assert.h>

char * my_strchr( char const *str, char c )
{
	assert( str != NULL );
	while( *str != '\0')
	{
		if( *str == c )
			return str;
		else
			str++;
	}
	printf("沒有找到該字元 \n");
	return 0;
}

int main()
{
	char *str = "dandanwa";
	char ch = 's';
	printf("%s\n",my_strchr( str,ch ));
	return 0;
}

查詢a的話是這樣的:

查詢s就會返回空