长字符串是否包含短字符串

20170928宜信软件研发编程题:

    对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始)。如果不存在,则返回 -1。

1、使用string类中自定义函数:

使用字符串string自带查找函数find();find()查找的是字符串第一个字符,并 不是整个字符串,要判断是否取到短字符串的整个位置;

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string str1, str2;
	while (cin >> str1 >> str2)
	{
		if (str1.empty() || str2.empty())
			cout << -1;
		if (str1.size() < str2.size())
		{
			string str;
			str = str1;
			
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要判断一个字符串是否包含另一个字符串,可以使用编程语言提供的字符串函数。以下是几种常用的方法: 1. 使用in关键字(Python语言):可以使用in关键字来判断一个字符串是否包含另一个字符串。例如,在Python中,可以这样写代码: ```python string1 = "hello world" string2 = "world" if string2 in string1: print("string1 包含 string2") else: print("string1 不包含 string2") ``` 以上代码会输出"string1 包含 string2",因为"world"是包含在"hello world"这个字符串中的。 2. 使用indexOf方法(JavaScript语言):可以使用indexOf方法来判断一个字符串是否包含另一个字符串。例如,在JavaScript中,可以这样写代码: ```javascript var str = "测试一个字符串111是否包含另外一个字符串"; if (str.indexOf("111") >= 0) { console.log('字符串包含111字符串'); #### 引用[.reference_title] - *1* [判断一个字符串是否包含另一个字符串](https://blog.csdn.net/qq_24147051/article/details/78889587)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [如何判断字符串是否包含另一个字符串](https://blog.csdn.net/weixin_42602726/article/details/129605568)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值