查找字符串是否以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
热门推荐
10 新人红包祝福语简短精辟
11 幼师给毕业祝福语简短
12 生日高考祝福语妹妹简短
13 下飞机祝福语简短英文
14 元宵发客户祝福语简短
15 爸妈金婚文案祝福语简短
16 新郎朋友红包祝福语简短
17 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短