不使用strcat函数,将两个字符串连接起来
时间: 2024-06-14 10:06:55 浏览: 5
以下是一个不使用strcat函数将两个字符串连接起来的C语言代码示例:
```c
#include <stdio.h>
#include <string.h>
#define N 30
#define M 10
int main() {
int i, j;
char a[N] = {'\0'}, b[M] = {'\0'};
puts("输入第一个字符串:");
gets(a);
puts("输入第二个字符串:");
gets(b);
/*--将第二个字符串接在第一个字符串后--*/
for (i = strlen(a) / sizeof(a[0]), j = 0; j < strlen(b) / sizeof(b[0]); i++, j++) {
a[i] = b[j];
}
puts("两字符串链接后为:");
puts(a);
return 0;
}
```
这段代码首先定义了两个字符数组a和b,并初始化为空。然后通过输入函数gets()分别获取用户输入的两个字符串。接下来,通过for循环将第二个字符串b中的每个字符依次拼接到第一个字符串a的末尾。最后,使用puts()函数输出拼接后的字符串a。
相关问题
用strcat函数将两个字符串连接起来
Sure! 您可以使用strcat函数将两个字符串连接起来。```char str1[20] = "Hello"; char str2[20] = "World"; strcat(str1, str2); printf("%s", str1);``` 意思是将"World"连接到"Hello"的后面,输出结果是"HelloWorld"。
C语言用strcat函数将两个字符串连接起来
可以使用 strcat 函数将两个字符串连接起来,例如:
```c
char str1[20] = "hello";
char str2[20] = "world";
strcat(str1, str2);
printf("%s\n", str1); // 输出 "helloworld"
```