查找字符串是否以C ++中的另一个给定字符串开头和结尾
在此问题中,我们给了两个字符串str和corStr。我们的任务是查找一个字符串是否以另一个给定的字符串开头和结尾。
让我们举个例子来了解这个问题,
输入: str=“abcprogrammingabc”conStr=“abc”
输出: 真
解决方法:
要解决该问题,我们需要检查字符串是否以conStr开始和结束。为此,我们将找到string和corStr的长度。然后,我们将检查len(String)>len(conStr),如果不是,则返回false。
检查大小为corStr的前缀和后缀是否相等,并检查它们是否包含corStr。
该程序说明了我们解决方案的工作原理,
示例
#include <bits/stdc++.h>
using namespace std;
bool isPrefSuffPresent(string str, string conStr) {
int size = str.length();
int consSize = conStr.length();
if (size < consSize)
return false;
return (str.substr(0, consSize).compare(conStr) == 0 && str.substr(size-consSize, consSize).compare(conStr) == 0);
}
int main() {
string str = "abcProgrammingabc";
string conStr = "abc";
if (isPrefSuffPresent(str, conStr))
cout<<"The string starts and ends with another string";
else
cout<<"The string does not starts and ends with another string";
return 0;
}输出-
The string starts and ends with another string
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下