第1天----验证一个字符串是否是另一个字符串的子串(strstr()函数,find()函数)

本文我们将学习如何去验证一个字符串是否是另一个字符串的子串。

在这里插入图片描述


一、基本知识:

1. strstr()函数:

strstr函数是C语言中的字符串处理函数,用于在一个字符串中查找另一个字符串的第一次出现的位置。它的原型如下:

char *strstr(const char *haystack, const char *needle);

其中,参数haystack是源字符串,参数needle是要查找的目标字符串。

  • 函数的返回值是一个指向找到的目标字符串在原字符串中的首个字符的指针。如果未找到目标字符串,则返回NULL。

以下是一个使用strstr函数的例子:

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

int main() {
    char str1[] = "Hello, world!";
    char str2[] = "world";
    char *result;

    result = strstr(str1, str2);

    if (result != NULL) {
        printf("'%s' found in '%s' at position %ld\n", str2, str1, result - str1);
    } else {
        printf("'%s' not found in '%s'\n", str2, str1);
    }

    return 0;
}

输出结果为:

'world' found in 'Hello, world!' at position 7
  • 这个例子中,strstr函数找到了字符串"world"在字符串"Hello, world!"中的位置,并返回了指向该位置的指针。

2. std::find()函数:

C++中的string库中的std::find()函数也可以在一个字符串中查找另一个字符串的第一次出现的位置。并且std::string::find()函数会返回一个位置索引,如果找不到子串,则返回std::string::npos

  • 在C++中,std::string::npos是一个特殊的静态成员变量,它表示字符串中不存在指定的子串。
#include <iostream>
#include <string>
#include <algorithm>

int main() {
    std::string str1 = "Hello, world!";
    std::string str2 = "world";

    std::size_t found = str1.find(str2);

    if (found != std::string::npos) {
        std::cout << "'" << str2 << "' found in '" << str1 << "' at position " << found << std::endl;
    } else {
        std::cout << "'" << str2 << "' not found in '" << str1 << "'" << std::endl;
    }

    return 0;
}

输出结果为:

'world' found in 'Hello, world!' at position 7
  • 这个例子中,std::find()函数找到了字符串"world"在字符串"Hello, world!"中的位置,并返回了该位置的索引。如果未找到目标字符串,则返回std::string::npos

二、小试牛刀:

题目描述
输入两个字符串,验证其中一个串是否为另一个串的子串。

输入格式
两行,每行一个字符串。

输出格式

  • 若第一个串 s 1 是第二个串 s 2 的子串,则输出(s1) is substring of (s2);

  • 否则,若第二个串 s 2是第一个串 s 1的子串,输出(s2) is substring of (s1);

  • 否则,输出 No substring。

输入输出样例:
输入 1
abc
dddncabca
输出 1
abc is substring of dddncabca
输入2
aaa
bbb
输出 2
No substring
说明/提示:
对于 100%的数据,字符串长度在 20 以内。


方法一:

直接按照题目的思路来实现

  • 此方法虽然代码量大,但并不难想,完全是按照题目的要求来实现,所以不做过多解释。
#include<iostream>
#include<string>
using namespace std;

void substring(string s1, string s2,int len1,int len2,int min)
{
	int flag = 0;
	if (min == len1)
	{
		int k = 0;
		
		for (int i = 0; i < len2; i++)
		{
			int cnt = 0;
			
			for (int j = 0; j < min; j++)
			{
				if (s1[j] == s2[k])
				{
					cnt++;
					k++;

				}
				else break;
			}
			if (cnt == min) { flag = 1; break; }
			else flag = -1; 
			k = i;
		}
	}
	else
	{
		int k = 0;
		for (int i = 0; i < len1; i++)
		{
			int cnt = 0;
			for (int j = 0; j < min; j++)
			{
				if (s2[j] == s1[k])
				{
					cnt++;
					k++;
				}
				else break;
			}
			if (cnt == min){ flag = 0; break; }
			else  flag = -1; 
			k = i;
		}
	}
	if (flag == -1) cout << "No substring" << endl;
	else if(flag==0)  cout << s2 << " is substring of " << s1 << endl;
	else cout << s1 << " is substring of " << s2 << endl;

	

}

int main(void)
{
	string s1,s2;
	cin >> s1 >> s2;
	int len1 = s1.length();
	int len2 = s2.length();	
	int min= len1 <= len2 ? len1 : len2;
	substring(s1, s2,len1,len2,min);



	return 0;
}


方法二:

利用cstring 库中的 strstr 函数查找:

可以先用字符数组存字符串,然后查找子串可以使用 cstring 库中的 strstr 函数,未找到时返回 NULL。 先查找 b 是否是 a的子串,再查找a是否是 b 的子串。如果都不是,输出 "No substring"

  • 代码如下:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
	char a[25],b[25];
	cin>>a>>b;
	if(strstr(a,b)!=NULL)
		cout<<b<<" is substring of "<<a;
	else if(strstr(b,a)!=NULL)
		cout<<a<<" is substring of "<<b;
	else
		cout<<"No substring";
	return 0;
}


方法三:

利用 string 库里的函数来实现。

比如此时我们有一个 string 类型的变量 strstring str="hello world";
然后我们可以调用find函数来在 str中寻找是否含有子串"hello"str.find("hello")
如果该函数的返回值为str.npos(表示无效),则表示在 str 中不存在该子串,否则存在。

  • 代码如下:
#include <iostream>
#include <string>
using namespace std;
string a,b;
int main()
{
    cin>>a>>b;
    if(a.find(b)!=a.npos)    //如果b是a的子串 
    {
        cout<<b<<" is substring of "<<a<<endl;
    }
    else if(b.find(a)!=b.npos)   //如果a是b的子串 
    {
        cout<<a<<" is substring of "<<b<<endl;
    }
    else      //如果没有子串关系 
    {
        cout<<"No substring"<<endl;
    }
    return 0;
}


方法四:

使用C++string库中的std::find()函数也可以实现字符串的查找操作(此方法和方法三有点类似)
std::find()函数可以在一个字符串中查找另一个字符串的第一次出现的位置

  • 注意find函数若未找到的返回值为string::npos

代码如下:

#include<bits/stdc++.h>//万能头
using namespace std;
string a,b;
int main(){
	cin>>a>>b;
	if(b.find(a)!=string::npos) cout<<a<<" is substring of "<<b;//a是b的子串
	else if(a.find(b)!=string::npos) cout<<b<<" is substring of "<<a;//b是a的子串
	else cout<<"No substring";
	return 0;
}

三、拓展讲解:

1. 域解析符:

  • 聪明的你可能已经发现了,上面的方法三和方法四其实是一样的,而这将关系到作用域的问题。

域解析符(Scope resolution operator)是C++中的一个运算符,用于访问命名空间、类、结构体、枚举等作用域内的成员。

  • 域解析符使用两个冒号::表示,语法形式为命名空间名::成员名或类名::成员名。

以下是一些使用域解析符的示例:

  1. 访问命名空间的成员
namespace MyNamespace {
    int value = 10;
}

int main() {
    std::cout << MyNamespace::value << std::endl;
    return 0;
}

  1. 访问类的静态成员
class MyClass {
public:
    static int value;
};

int MyClass::value = 20;

int main() {
    std::cout << MyClass::value << std::endl;
    return 0;
}

  1. 访问类的成员函数
class MyClass {
public:
    void printHello() {
        std::cout << "Hello" << std::endl;
    }
};

int main() {
    MyClass obj;
    obj.printHello();
    return 0;
}
  • 在上述示例中,域解析符用于访问命名空间的成员、类的静态成员和类的成员函数。它可以帮助我们在不同的作用域中访问到所需的成员。

因此,a.find(b) != a.npos的判断条件也是正确的(a.npos其实就等同于string::npos),它用于判断字符串a中是否存在子串b。如果子串b存在于字符串a中,a.find(b)的返回值不等于std::string::npos,条件成立;否则,条件不成立。


  • 好了,今天的讲解就到这里了。我们今天主要学习了如何验证一个字符串是否是另一个字符串的子串,并对strstr(),std::find()函数等进行了一定的学习。如果你有任何关于本篇文章的疑问,都可以在评论区提出来,我会一一解答。明天继续加油!
    在这里插入图片描述
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

追逐远方的梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值