C/C++:子串判断
一、子串判断题目描述我们定义字符串包含关系字符串Aabc字符串Bab字符串Cac则说A包含BA和C没有包含关系。输入描述:两个字符串判断这个两个字符串是否具有包含关系测试数据有多组请用循环读入。输出描述:如果包含输出1否则输出0.输入abc ab输出11、C风格strstr()char *strstr(const char *str1, const char *str2);如果str2是str1的子串返回指针位置否则返回空指针。#include iostream #include string #include cstring using namespace std; int main() { string s1, s2; while(cin s1 s2) { if(s1.size() s2.size()) cout (strstr(s1.c_str(), s2.c_str()) ! nullptr) endl; //换行有坑 else cout (strstr(s2.c_str(), s1.c_str()) ! nullptr) endl; } return 0; }2、C风格find()string::size_type string::find(string );在string对象中查找参数string类型的字符串是否存在。如果存在返回起始位置string::size_type类型不存在则返回 string::npos。#include iostream #include string #include cstring using namespace std; int main() { string s1, s2; while(cin s1 s2) { if(s1.size() s2.size()) cout (s1.find(s2) ! string::npos) endl; else cout (s2.find(s1) ! string::npos) endl; } return 0; }