1. 编一个函数,将字符串s2连接到s1后面,不用strcat函数。(请填空)#include <stdio. h> #include <string. h> void mainO ( char s1[80], s2[40]; int i=0, j=0; gets(s1) ; gets(s2) ; /*查找定位s1的连接点(即结束符) */ 8灵静话/*将s2逐个字符复制到s1后,对应下标后移*/ /*手工对s1加结束符*/ printf( "The new string is:%s” ,s1);
时间: 2024-03-29 22:39:19 浏览: 80
#include <stdio.h>
#include <string.h>
void mystrcat(char s1[], char s2[]) {
int len1 = strlen(s1);
int len2 = strlen(s2);
int i, j;
for (i = len1, j = 0; j < len2; i++, j++) {
s1[i] = s2[j];
}
s1[i] = '\0';
}
int main() {
char s1[80], s2[40];
printf("Enter string s1: ");
gets(s1);
printf("Enter string s2: ");
gets(s2);
mystrcat(s1, s2);
printf("The new string is: %s\n", s1);
return 0;
}
相关问题
#include<iostream> #include<stdio.h> #include<string.h>这三个头文件的异同
这三个头文件都是C++中常用的头文件,其中<iostream>和<cstdio>是C++标准库的一部分,<string.h>则是C语言标准库中的头文件,在C++中也可以使用。
它们的异同点如下:
1. #include<iostream>:这个头文件定义了输入输出流(iostream)对象,例如cin和cout。它还定义了一些用于输入输出流的函数。
2. #include<stdio.h>(或者<cstdio>):这个头文件定义了C标准输入输出函数(如printf和scanf),以及文件操作相关的函数(如fopen和fclose)。
3. #include<string.h>:这个头文件定义了一些字符串处理函数,如strcpy和strcat。
它们的共同点是都提供了在程序中使用的函数和类型定义。同时,它们都是使用预编译指令#include来包含进程序中的。
#include <stdio.h> #include <stdlib.h> #include <string.h> //头文件
这三个头文件在C语言中分别扮演着不同的角色:
1. `#include <stdio.h>`: 这是标准输入输出库(Standard Input/Output Library)的包含指令,它提供了基本的文件输入输出功能,如`printf()`(输出格式化字符串)和`scanf()`(输入格式化数据)等,用于程序与用户交互或处理文件内容。
2. `#include <stdlib.h>`: 这个头文件包含了C语言的一些基本数据类型和内存管理函数,比如`malloc()`和`free()`,用于动态分配和释放内存,以及系统函数如`exit()`(退出程序)。
3. `#include <string.h>`: 字符串处理库的包含指令,提供了一系列字符串操作的函数,如`strlen()`(计算字符串长度)、`strcpy()`(复制字符串)、`strcat()`(拼接字符串)等,这些都是处理字符串操作的常用工具。
如果你正在编写C程序,这些头文件会为你的程序提供基本的功能,例如输入用户输入、动态内存管理,以及处理字符串数据。如果你想要深入了解这些头文件中的具体函数和用法,可以提问相关的函数如何使用或者它们的常见应用场景是什么。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)