编写一个函数 char *strfind(char *s, char *t),用于查找字符串t在字符串s中最右边出现的位置,如果没有找到则返回NULL

编写一个函数 char *strfind(char *s, char *t),用于查找字符串t在字符串s中最右边出现的位置,如果没有找到则返回NULL

以下代码原型来自https://blog.csdn.net/hannea/article/details/10163975
本人仅对代码稍作修改,侵删。

#include <iostream>
#include <string>
using namespace std;
 
char *strfind(char *src, char *dst)
{
	int len1 = strlen(src);
	int len2 = strlen(dst);
	int start = len1 - 1;
	int end = len2 - 1;
	int i,j = len2 - 1,k=0;
	for(i = start;i >= 0;i--)
	{
		if(src[i] == dst[j])
		{
			j--;k++;
			if(j == - 1)
			{
				cout << "the position is: " << i + 1 << endl;
				return &src[i];
			}
		}
		else
		{j = len2 - 1;i+=k;k=0;}
	}
	return NULL;
}
 
int main()
{
	char src[100];
	char dst[100];
	cout << "请输入一个字符串:" << endl;
	cin.getline(src,100,'\n');
	cin.clear();
	cout << "请输入要查找的字符串:" << endl;
	cin.getline(dst,100,'\n');
 
	if(strfind(src, dst) != NULL)
	{
		cout << "所寻找的字符串以及后面的字符串为:" << strfind(src, dst) << endl;
	}
	else 
		cout << "no such characters!" << endl;
	system("pause");
	return 0;
}

在原博主的代码上,有些小欠缺,当出现下面情况时,无法做正确的判断:
在这里插入图片描述
红笔划线处为修改
在这里插入图片描述
结果为
在这里插入图片描述
原博主文中还介绍了以下语句的用法:
1、cin
2、cin.get()
3、cin.getline()
4、getline()
5、gets()
6、getchar()
有兴趣的可前往https://blog.csdn.net/hannea/article/details/10163975
此文仅为兴趣使然,也当作学习笔记,如有不妥之处,望各位不吝赐教。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值