验证字串c&c++

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

输入格式
输入两个字符串, 每个字符串占一行,长度不超过 200200 且不含空格。

输出格式
若第一个串 s_1s1​ 是第二个串 s_2s2​ 的子串,则输出"(s1) is substring of (s2)";
否则,若第二个串 s2是第一个串s1的子串,输出"(s2) is substring of (s1)";
否则,输出"No substring"。

Sample Inputabc
dddncabca
Sample Output
abc is substring of dddncabcaSponsor

借用到了c++中的find函数和c中的strstr函数

c++

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
string s1,s2;
int main()
{

	cin>>s1>>s2;
	if(s1.find(s2)!=s1.npos)
		cout<<s2<<" is substring of "<<s1<<endl;
	else if(s2.find(s1)!=s2.npos)
		cout<<s1<<" is substring of "<<s2<<endl;
	else
		cout<<"No substring"<<endl;
	return 0;
}

c

#include<stdio.h>
#include<string.h>
int main()
{
	char s1[200],s2[200];
	scanf("%s%s",s1,s2);
	char *r1,*r2;
	r1=strstr(s2,s1);
	r2=strstr(s1,s2);
	if(r1!=NULL)
		printf("%s is substring of %s\n",s1,s2);
	else if(r2!=NULL)
		printf("%s is substring of %s\n",s2,s1);
	else
		printf("No substring\n");
	return 0;
}

strstr函数,如果str1中存在str2则返回 str1 中首次出现 str2 的位置的指针,如果 str1 中不存在 str2 则返回 NULL。(指针是一个地址)

库函数yyds,堆堆加油⛽️

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

元堆堆

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

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

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

打赏作者

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

抵扣说明:

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

余额充值