char* strstr
时间: 2024-03-13 12:40:19 浏览: 25
`char* strstr` 是一个C语言中的字符串处理函数,用于在一个字符串中找另一个字符串的第一次出现位置。它的函数原型如下:
```c
char* strstr(const char* str1, const char* str2);
```
其中,`str1` 是要被搜索的字符串,`str2` 是要搜索的目标字符串。函数返回一个指向第一次出现目标字符串的位置的指针,如果没有找到目标字符串,则返回`NULL`。
这个函数会从`str1`的起始位置开始逐个字符地与`str2`进行比较,直到找到第一次出现目标字符串或者遍历完整个字符串。如果找到了目标字符串,返回的指针指向该位置;如果没有找到,返回`NULL`。
需要注意的是,`strstr` 函数是区分大小写的,如果需要进行大小写不敏感的搜索,可以使用其他函数进行处理。
相关问题
C语言标准函数库中包括 strstr 函数,在主串中查找子串。作为练习,我们自己编写一个功能与之相同的函数。 函数原型 char* StrStr(const char *txt, const char *pat); 说明:txt 和 pat 分别为主串和子串的起始地址。若查找成功,则函数值为子串在主串中首次出现的起始地址,否则函数值为NULL。 特别地,我们对C语言库函数strstr进行适当修改:若子串为空串,则没有意义,函数值规定为NULL。 裁判程序 #include <stdio.h> #include <string.h> char* StrStr(const char *txt, const char *pat); int main() { char m[1024], s[1024], *p; gets(m); gets(s); p = StrStr(m, s); if (p) { printf("%d\n", p - m); } else { puts("NULL"); } return 0; } /* 你提交的代码将被嵌在这里 */ 输入样例1 This is a pencil is 输出样例1 2 输入样例2 This is a pencil be 输出样例2 NULL
以下是一个实现功能与 `strstr` 函数相同的 `StrStr` 函数的示例代码:
```c
char* StrStr(const char *txt, const char *pat) {
if (*pat == '\0') { // 若子串为空串,则返回 NULL
return NULL;
}
while (*txt != '\0') { // 在主串中逐个匹配子串
const char *t = txt;
const char *p = pat;
while (*t != '\0' && *p != '\0' && *t == *p) {
t++;
p++;
}
if (*p == '\0') { // 若子串已经匹配完成,则返回子串在主串中首次出现的起始地址
return (char*)txt;
}
txt++; // 否则继续在主串中查找下一次匹配的位置
}
return NULL; // 若主串中没有找到子串,则返回 NULL
}
```
该函数首先判断子串是否为空串,如果是,则返回 NULL。然后在主串中逐个匹配子串,如果匹配成功,则返回子串在主串中首次出现的起始地址,否则继续在主串中查找下一次匹配的位置。如果在主串中没有找到子串,则返回 NULL。
使用示例:
```c
#include <stdio.h>
#include <string.h>
char* StrStr(const char *txt, const char *pat);
int main() {
char m[1024], s[1024], *p;
gets(m);
gets(s);
p = StrStr(m, s);
if (p) {
printf("%d\n", p - m);
} else {
puts("NULL");
}
return 0;
}
```
c++,完成程序,只在Program和End之间增加代码。 英语单词的后缀构成有一定的规律,例如”teacher”和”programmer”有相同后缀”er”。请完成如下的C++函数功能:findCommonSuffix(char * word1, char * word2, char * suffix),该函数分析得到word1和word2两个单词的最长共同后缀,分析结果存到参数suffix中。 例如,”globally”和”internally”的最长共同后缀是”ally”,”gloves”和”dove”的最长共同后缀是空串。 提示:可能用到的字符串函数说明如下: strlen(const char * str):返回字符串str的长度; strcpy(char * dest, const char * src):把字符串src复制到dest; strcat(char * dest, const char * src):把字符串src连接到dest后面; char * strstr(const char * s1, const char * s2):从s1中查找子串s2第一次出现的位置。
以下是实现该功能的C++代码:
```cpp
#include <iostream>
#include <cstring>
using namespace std;
void findCommonSuffix(char *word1, char *word2, char *suffix) {
int len1 = strlen(word1);
int len2 = strlen(word2);
int minLen = min(len1, len2);
int i;
for (i = 1; i <= minLen; i++) {
if (strcmp(word1 + len1 - i, word2 + len2 - i) != 0) {
break;
}
}
if (i == 1) {
suffix[0] = '\0';
} else {
strcpy(suffix, word1 + len1 - i + 1);
}
}
int main() {
char word1[] = "globally";
char word2[] = "internally";
char suffix[100];
findCommonSuffix(word1, word2, suffix);
cout << suffix << endl;
char word3[] = "gloves";
char word4[] = "dove";
findCommonSuffix(word3, word4, suffix);
cout << suffix << endl;
return 0;
}
```
该函数首先获取两个单词的长度,然后比较它们的末尾字符是否相同,直到找到第一个不相同的字符。这个字符之后的部分就是它们的最长共同后缀。如果没有共同后缀,则返回空串。
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)