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

这篇博客总结了C++中 cin、cin.get()、cin.getline()、getline() 和 gets() 函数的用法,包括如何接收字符和字符串,如何处理空格,并通过示例代码详细解释了每个函数的特性。文章还提到了这些函数在处理带有空格的输入时的区别。
摘要由CSDN通过智能技术生成
#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;
	for(i = start;i >= end;i--)
	{
		if(src[i] == dst[j])
		{
			j--;
			if(j == - 1)
			{
				cout << "the position is: " << i + 1 << endl;
				return &src[i];
			}
		}
		else
			j = len2 - 1;
	}
	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;
}

输出结果如下:


一:cin、cin.get()、cin.getline()、getline()、gets()等函数的用法:

原文地址:http://hi.baidu.com/ybhuxiao/item/f767fafb9dcb32ee1b111f81

学C++的时候,这几个输入函数弄的有点迷糊;这里做个小结,为了自己复习,也希望对后来者能有所帮助,如果有差错的地方还请各位多多指教(本文所有程序均通过VC 6.0运行)转载请保留作者信息;
1、cin
1、cin.get()
2、cin.getline()
3、getline()
4、gets()
5、getchar()

1、cin>>          

用法1:最基本,也是最常用的用法,输入一个数字:

#include <iostream>
using namespace std;
main ()
{
int a,b;
cin>>a>>b;
cout<<a+b<<endl;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值