C++ 实现beginwith(),endwith()

网友投稿 766 2022-11-19

C++ 实现beginwith(),endwith()

C++ 实现beginwith(),endwith()

实现代码

#include#includeusing namespace std;int endswith(string s, string sub) { if (s.rfind(sub) == -1) {//排除出现类似s:23 sub:123的情况. return 0; } else { return s.rfind(sub) == (s.length() - sub.length()) ? 1 : 0; }}int startswith(string s, string sub) { return s.find(sub) == 0 ? 1 : 0;}int main(){ string str = "Helloworld"; string str1 = "He"; string str2 = "ld"; if (startswith(str, str1)) { cout << str << " startwith " << str1 << endl; } if (endswith(str, str2)) { cout << str << " endwith " << str2 << endl; } return 0;}

运行结果

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Springboot框架整合添加redis缓存功能
下一篇:UVA122 树的层次遍历 Trees on the level
相关文章

 发表评论

暂时没有评论,来抢沙发吧~