查找字符串是否以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 婆婆高寿祝福语大全简短