在指定字符串中查找第一次找到所查找的字符和最后一次查找的到的字符

<span style="font-family: Arial, Helvetica, sans-serif;">1:最后一次查找到字符</span></span>
#include<stdio.h>
char const *strrchr(char const *str,int ch)
{
	char const *ret=NULL;
	int n=0;
	while(*str!='\0')
	{
		if(*str==ch)
		{
			ret=str;
			str++;
			n++;
		}
		else
			str++;
	}
	if(0==n)
		return NULL;
	else 
		return ret;
}
void main()
{
	char *str="hello e word";
	int ch='w';
	char const *ret=(char const *)strrchr(str,ch);
	if(ret==NULL)
		printf("返回值为:NULL\n");
	else
		printf("%c\n",*ret);
}
<span style="font-family: Arial, Helvetica, sans-serif;">2:第一次查找到字符</span>
#include<stdio.h>
char const *strchr(char const *str,int ch)
{
	while(*str!='\0')
	{
		if(*str==ch)
			return str;
		else
			str++;
	}
	
	return NULL;
}
void main()
{
	char *str="hello word";
	int ch='e';
	char const *ret=(char const *)strchr(str,ch);
	if(ret==NULL)
		printf("返回值为:NULL\n");
	else
		printf("%c\n",*ret);
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值