用指针将两个字符串连接起来
时间: 2024-03-15 15:43:54 浏览: 30
可以使用指针和循环来将两个字符串连接起来,具体实现如下:
```c
#include <stdio.h>
#include <string.h>
int main()
{
char s1[100] = "Hello, ";
char s2[] = "world!";
char *p1 = s1 + strlen(s1); // p1指向s1的结尾位置
char *p2 = s2; // p2指向s2的开头位置
while (*p2 != '\0') { // 将s2的字符逐一拷贝到s1的结尾位置
*p1++ = *p2++;
}
*p1 = '\0'; // 在s1的结尾位置添加字符串结束符
printf("%s\n", s1); // 输出:Hello, world!
return 0;
}
```
上述代码中,我们首先定义了两个字符串 `s1` 和 `s2`,其中 `s1` 已经包含了一部分内容。然后我们定义了两个指针 `p1` 和 `p2`,分别指向 `s1` 中的结尾位置和 `s2` 的开头位置。接着我们使用循环将 `s2` 中的字符逐一拷贝到 `s1` 的结尾位置,直到遇到字符串结束符 `\0`。最后再在 `s1` 的结尾位置添加一个字符串结束符 `\0`,即可完成字符串的连接。
相关问题
用指针函数,将两个字符串连接起来
可以使用指针函数来实现两个字符串的连接。
下面是一个简单的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* concat(char* str1, char* str2);
int main() {
char str1[20] = "Hello, ";
char str2[20] = "world!";
char* result = concat(str1, str2);
printf("%s\n", result);
free(result); // 释放内存
return 0;
}
char* concat(char* str1, char* str2) {
int len1 = strlen(str1);
int len2 = strlen(str2);
char* result = (char*)malloc((len1 + len2 + 1) * sizeof(char)); // 分配内存
if (result == NULL) {
printf("Error: memory allocation failed.\n");
exit(1);
}
strcpy(result, str1);
strcat(result, str2);
return result;
}
```
这个程序定义了一个 `concat()` 函数,该函数接受两个字符串指针参数,并返回一个新的字符串指针,该指针指向连接后的字符串。该函数首先计算两个字符串的长度,然后分配足够的内存来存储连接后的字符串,使用 `strcpy()` 和 `strcat()` 函数将两个字符串拼接起来,最后返回新字符串的指针。在主函数中,我们使用 `concat()` 函数将两个字符串连接起来,并打印结果。注意,在使用完字符串后,需要使用 `free()` 函数释放分配的内存,以避免内存泄漏的问题。
自定义函数将两个字符串连接起来指针实现
可以使用指针实现一个自定义函数,将两个字符串连接起来。以下是示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 自定义函数,将两个字符串连接起来
char* strconcat(char* str1, char* str2) {
int len1 = 0, len2 = 0, i, j;
// 计算 str1 的长度
while (*(str1 + len1) != '\0') {
len1++;
}
// 计算 str2 的长度
while (*(str2 + len2) != '\0') {
len2++;
}
// 分配内存
char* result = (char*)malloc((len1 + len2 + 1) * sizeof(char));
// 拷贝 str1 到 result
for (i = 0; i < len1; i++) {
*(result + i) = *(str1 + i);
}
// 拷贝 str2 到 result
for (j = 0; j < len2; j++) {
*(result + i + j) = *(str2 + j);
}
// 加上字符串结束符
*(result + i + j) = '\0';
return result;
}
int main() {
char str1[] = "Hello, ";
char str2[] = "world!";
char* result = strconcat(str1, str2);
printf("%s\n", result);
free(result); // 释放内存
return 0;
}
```
在上面的代码中,我们先分别计算了两个字符串的长度,然后根据长度分配了内存。接着,使用指针遍历 str1 和 str2,将它们拷贝到 result 中。最后,加上一个字符串结束符,并返回 result。需要注意的是,我们在使用完 result 后,需要手动释放它所占用的内存,以免造成内存泄漏。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)