检测字符串是否位于另一个字符串尾端

#include <stdio.h>
#include <stdbool.h>
#include <string.h>

/*寻找子串出现的最后的位置*/
char *strrstr(const char *string, const char *ending){
	char *pos, *last;
	int len;
	
	len = strlen(ending);
	pos = strstr(string, ending);	/*出现第一个位置*/
	if(NULL == pos)
		return NULL;
	
	/*找到最后出现的位置*/
	last = pos + len;
	while(1){
		pos = strstr(last, ending);
		if(NULL == pos)
			break;
		last = pos + len;
	}
	return last - len;
}

bool solution(const char *string, const char *ending){
	char *pos;
	int len;
	if(*ending == '\0')	/*子串""是任何字符串的尾端*/
		return true;

	len = strlen(ending);
	pos = strrstr(string, ending);
	if(!pos || *(pos + len) != '\0')	/*检测是否在尾端*/
		return false;
	return true;
}

int main(){
	const char *s = "babbabbab";
	const char *e = "bab";
	
	if(!solution(s, e))
		puts("no");
	else
		puts("yes");
	return 0;
}

 

转载于:https://www.cnblogs.com/mocuishle/p/8076362.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值