再来一个strstr函数示例
时间: 2024-06-26 19:01:05 浏览: 11
`strstr` 函数是 C 语言中的字符串搜索函数,用于在一个字符串中查找指定子串第一次出现的位置。如果找到,它返回子串在原字符串中的首地址;如果没有找到,返回 `NULL`。
以下是一个简单的 `strstr` 函数示例:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char substr[] = "World";
// 查找子串在原字符串中的位置
char *result = strstr(str, substr);
if (result) {
printf("Substring '%s' found at position %ld\n", substr, result - str);
} else {
printf("Substring '%s' not found in the string.\n", substr);
}
return 0;
}
```
在这个例子中,`strstr(str, substr)` 将在 `str` 中查找子串 `"World"`。如果找到,它会返回指向该子串起始位置的指针,计算后输出其在原字符串中的索引(注意在 C 语言中,索引从 0 开始)。如果没找到,`result` 将为 `NULL`,并打印相应的提示信息。
相关问题
strstr函数示例
`strstr` 是 C 语言中的一个字符串搜索函数,用于在一个字符串中查找指定子串首次出现的位置。这个函数在 `<string.h>` 头文件中定义。函数原型如下:
```c
char *strstr(const char *haystack, const char *needle);
```
- `haystack`:要在其中查找子串的原始字符串(也称为“主串”)。
- `needle`:要在 `haystack` 中查找的子字符串。
如果找到了匹配的子串,`strstr` 函数返回指向子串的第一个字符的指针;如果没有找到,函数会返回 `NULL`。
这里有一个简单的示例:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char substr[] = "World";
if (strstr(str, substr)) {
printf("Substring found at position %ld\n", (long)strstr(str, substr) - str);
} else {
printf("Substring not found.\n");
}
return 0;
}
```
在这个例子中,`strstr(str, substr)` 会返回 `str + 7`,因为 "World" 在 "Hello, World!" 中的位置是第8个字符开始,所以位置是 `7`(从0开始计数)。如果子串不存在,`strstr` 将返回 `NULL`,然后程序会打印 "Substring not found."。
实现一个不区分大小写的strstr函数
实现一个不区分大小写的strstr函数可以通过以下步骤来完成:
1. 首先,将目标字符串和模式字符串都转换为小写或大写字母,以便进行大小写不敏感的比较。
2. 使用两个指针,一个指向目标字符串,一个指向模式字符串。
3. 在目标字符串中循环遍历,直到找到与模式字符串的第一个字符相等的字符。
4. 从当前位置开始,比较目标字符串和模式字符串中的字符,如果不相等,则继续遍历目标字符串。
5. 如果找到了与模式字符串匹配的子串,则返回该子串在目标字符串中的起始位置。
6. 如果遍历完整个目标字符串都没有找到匹配的子串,则返回NULL。
下面是一个示例代码实现:
```c++
#include <iostream>
#include <cstring>
#include <cctype>
char* stristr(const char* haystack, const char* needle) {
if (haystack == nullptr || needle == nullptr) {
return nullptr;
}
int haystackLen = strlen(haystack);
int needleLen = strlen(needle);
for (int i = 0; i <= haystackLen - needleLen; i++) {
int j;
for (j = 0; j < needleLen; j++) {
if (tolower(haystack[i + j]) != tolower(needle[j])) {
break;
}
}
if (j == needleLen) {
return const_cast<char*>(&haystack[i]);
}
}
return nullptr;
}
int main() {
const char* haystack = "Hello, World!";
const char* needle = "world";
char* result = stristr(haystack, needle);
if (result != nullptr) {
std::cout << "Found at position: " << result - haystack << std::endl;
} else {
std::cout << "Not found." << std::endl;
}
return 0;
}
```
相关推荐
![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_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)