|
发表于 2024-4-9 21:34:22
|
显示全部楼层
山西省太原市
#include <iostream>
#include <string>
bool isSubstring(const std::string& str, const std::string& substr) {
return str.find(substr) != std::string::npos;
}
int main() {
std::string str = "Hello, world!";
std::string substr1 = "world";
std::string substr2 = "goodbye";
if (isSubstring(str, substr1)) {
std::cout << "子字符串 \"" << substr1 << "\" 在字符串 \"" << str << "\" 中找到了。" << std::endl;
} else {
std::cout << "子字符串 \"" << substr1 << "\" 没有在字符串 \"" << str << "\" 中找到。" << std::endl;
}
if (isSubstring(str, substr2)) {
std::cout << "子字符串 \"" << substr2 << "\" 在字符串 \"" << str << "\" 中找到了。" << std::endl;
} else {
std::cout << "子字符串 \"" << substr2 << "\" 没有在字符串 \"" << str << "\" 中找到。" << std::endl;
}
return 0;
}
|
|