#include <iostream> #include <string> #include <cstring> #include <ctype.h> std::string solution(std::string words, std::string msg){ std::string result; // TODO: int tStrLenW = words.length(); int tStrLenM = msg.length(); int tWordsFlag = 0; int tMsgFlag = 0; char* pCharText = new char[tStrLenW + 1]; memset(pCharText, 0, tStrLenW + 1); while(tStrLenW > tWordsFlag && tStrLenM > tMsgFlag) { if(tolower(words[tWordsFlag]) == tolower(msg[tMsgFlag])) { pCharText[tMsgFlag] = msg[tMsgFlag]; //strcat(pCharText, &msg[tMsgFlag]); tMsgFlag++; } else { tWordsFlag++; tMsgFlag = 0; } } if(pCharText == msg) result = "Yes"; else result = "No"; return result; } int main() { std::string words; std::string msg; getline(std::cin, words);; getline(std::cin, msg);; std::string result = solution(words, msg); std::cout<<result<<std::endl; return 0; }
时间: 2024-02-16 13:27:01 浏览: 27
这段代码的作用是读入两个字符串words和msg,然后在msg中找出与words匹配的子串,并判断是否完全匹配。如果完全匹配,则返回"Yes",否则返回"No"。
具体实现是先将words和msg的长度存入变量tStrLenW和tStrLenM中,并初始化tWordsFlag和tMsgFlag为0。然后动态申请一个长度为tStrLenW+1的字符数组pCharText,并将其全部初始化为0。
接下来使用while循环,在tStrLenW和tStrLenM均未超出范围的情况下,比较words[tWordsFlag]和msg[tMsgFlag]是否相等。如果相等,则将msg[tMsgFlag]赋值给pCharText[tMsgFlag],并将tMsgFlag加1。如果不相等,则将tWordsFlag加1,并将tMsgFlag重置为0。
循环结束后,再判断pCharText是否与msg完全匹配。如果是,则将result赋值为"Yes",否则将result赋值为"No",最后返回result。
需要注意的是,该代码中使用了一些C语言的库函数,如cstring和ctype.h,因此需要使用g++等支持C++11的编译器进行编译。
相关问题
#include<iostream>std;
`#include<iostream>`是C++程序中的预处理指令,它包含了iostream库。iostream库提供了C++程序中处理标准输入/输出流的基本功能。这个库中定义了一些重要的类和对象,以及与输入/输出相关的函数。
`std::cout`是iostream库中的一个对象,它是std::ostream类的一个实例,表示标准输出流(通常是屏幕输出)。通过使用`std::cout`对象,我们可以将数据输出到控制台。
以下是一个简单的示例,演示了如何使用`#include<iostream>`和`std::cout`来输出一条消息到控制台:
```cpp
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
```
上述代码中,`#include<iostream>`用于包含iostream库,`std::cout`用于输出字符串"Hello, World!"到控制台,`std::endl`用于在输出结束后换行。
统一文件名只用#include<iostream> #include<string.h>
这两个头文件的作用不同,<iostream> 是用于输入输出流的库,而<string.h> 是用于字符串处理的库。如果你的程序中需要使用字符串相关函数,如strcpy、strlen等,就需要用到<string.h>头文件。如果要使用cout和cin等输入输出流对象则需要使用<iostream>头文件。如果你两个都需要用到,那么你就需要同时包含这两个头文件。例如:
```cpp
#include <iostream>
#include <string.h>
using namespace std;
int main() {
char str1[10] = "hello";
char str2[10] = "world";
strcat(str1, str2); // 使用<string.h>中的字符串拼接函数strcat
cout << str1 << endl; // 使用<iostream>中的输出流对象cout
return 0;
}
```
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)