在此问题中,我们给了两个字符串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