strstr strcat 的讲解
时间: 2023-11-26 18:47:59 浏览: 70
strstr函数用于在一个字符串中查找另一个字符串出现的位置,其函数原型为:
```c
char *strstr(const char *str1, const char *str2);
```
其中,str1为要查找的字符串,str2为要查找的子字符串。如果找到了子字符串,则返回子字符串在字符串中第一次出现的位置的指针;如果没有找到,则返回NULL。
以下是一个示例代码:
```c
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "hello world";
char str2[10] = "world";
char *res = strstr(str1, str2);
if (res != NULL)
{
printf("The substring is found at position: %ld\n", res - str1);
}
else
{
printf("The substring is not found.\n");
}
return 0;
}
```
strcat函数用于将一个字符串追加到另一个字符串的末尾,其函数原型为:
```c
char *strcat(char *strDest, const char *strSrc);
```
其中,strDest为目标字符串,strSrc为要追加的字符串。该函数将strSrc追加到strDest的末尾,并返回strDest的指针。
以下是一个示例代码:
```c
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "hello";
char str2[10] = "world";
strcat(str1, str2);
printf("The concatenated string is: %s\n", str1);
return 0;
}
```
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)