用c语言写一函数,实现2个字符串的连接,函数形参用指针变量。例如"abcd"和"e12345",2个字符串,字符串的连接的结果为:"abcde12345"。
时间: 2024-04-29 15:26:43 浏览: 15
好的,这是一个关于C语言函数的问题。下面是一个示例函数,可以实现两个字符串的连接:
```c
void string_concat(char *str1, char *str2) {
while (*str1 != '\0') { // 遍历第一个字符串
str1++;
}
while (*str2 != '\0') { // 将第二个字符串接到第一个字符串的末尾
*str1 = *str2;
str1++;
str2++;
}
*str1 = '\0'; // 最后需要添加字符串结束符
}
```
调用示例函数:
```c
char str1[] = "abcd";
char str2[] = "e12345";
string_concat(str1, str2);
printf("%s", str1);
```
输出结果为:
```
abcde12345
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)