在C语言中,字符串存储为字符数组,以’\0’结束。 在C的接口中,有strstr(…)函数,可以在字符串中查找另一个字符串。
| char strstr(const char str1, const char *str2); |
|---|
功能为在str1中查找str2,如果存在,那么返回查找到的起始指针,否则返回NULL。
参考代码:
| #include #include #include using namespace std; int main() { string a = “abcdefghigklmn”; char b = “def”; char c = “123”; if (strstr(a.c_str(), b) == NULL) cout << “not found\n”; else cout << “found\n”; if (strstr(a.c_str(), c) == NULL) cout << “not found\n”; else cout << “found\n”; return 0; } |
|---|
