输入两个字符串,验证其中一个串是否为另一个串的子串。
输入格式
输入两个字符串, 每个字符串占一行,长度不超过 200 且不含空格。
输出格式
若第一个串 s1 是第二个串 s2 的子串,则输出"(s1) is substring of (s2)"
;
否则,若第二个串 s2是第一个串s1的子串,输出"(s2) is substring of (s1)"
;
否则,输出"No substring"
。
输出时每行末尾的多余空格,不影响答案正确性。
样例输入
abc
dddncabca
样例输出
abc is substring of dddncabca
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <iostream> #include <string> using namespace std; int main(){ string a,b; cin>>a>>b; if(a.find(b)!=-1){ cout<<b<<" is substring of "<<a; }else if(b.find(a)!=-1){ cout<<a<<" is substring of "<<b; }else{ cout<<"No substring"; } return 0; }
C++中find函数的用法:
1、查找第一次出现的目标字符串:
eg:
string s1 = "abcdef";
string s2 = "de";
int ans = s1.find(s2) ; //在S1中查找子串S2
如果查找成功则输出查找到的第一个位置,否则返回-1;
2、查找从指定位置开始的第一次出现的目标字符串:
eg:
string s1 = "abcdef";
string s2 = "de";
int ans = s1.find(s2, 2) ; //从S1的第二个字符开始查找子串S2
最后
以上就是醉熏枫叶最近收集整理的关于1、验证子串的全部内容,更多相关1、验证子串内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复