不使用strcat将两个字符串连接起来用c语言
时间: 2024-06-13 07:10:02 浏览: 107
以下是用C语言实现不使用strcat函数实现连接两个字符串的方法:
```c
#include <stdio.h>
#include <string.h>
#define MAX_LEN 100
void connect(char str1[], char str2[]) {
int len1 = strlen(str1);
int len2 = strlen(str2);
if (len1 + len2 >= MAX_LEN) {
printf("Error: The length of the connected string exceeds the maximum length!\n");
return;
}
for (int i = 0; i < len2; i++) {
str1[len1 + i] = str2[i];
}
str1[len1 + len2] = '\0';
}
int main() {
char str1[MAX_LEN], str2[MAX_LEN];
printf("Please input the first string: ");
scanf("%s", str1);
printf("Please input the second string: ");
scanf("%s", str2);
connect(str1, str2);
printf("The connected string is: %s\n", str1);
return 0;
}
```
阅读全文