不用自定义函数可以吗?
时间: 2023-11-19 10:05:03 浏览: 46
当然可以!以下是不使用自定义函数的 C++ 实现代码:
```cpp
#include <iostream>
#include <string>
int main() {
std::string s1, s2;
std::cout << "请输入两个字符串:" << std::endl;
std::cin >> s1 >> s2;
if (s1.length() != s2.length()) {
std::cout << "NO" << std::endl;
return 0;
}
std::string concatenated = s1 + s1; // 将 s1 连接自身
if (concatenated.find(s2) != std::string::npos) {
std::cout << "YES" << std::endl;
} else {
std::cout << "NO" << std::endl;
}
return 0;
}
```
在这个代码中,我们首先获取用户输入的两个字符串 `s1` 和 `s2`。然后我们检查两个字符串的长度是否相同,如果不相同则输出 "NO" 并返回。接下来,我们将字符串 `s1` 连接自身并存储在 `concatenated` 变量中,然后使用 `find` 函数在 `concatenated` 中查找子串 `s2`。如果找到了子串,则输出 "YES",否则输出 "NO"。
希望这次回答符合您的要求!如果还有其他问题,请随时提问。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)