编写带有函数的程序,函数表达式fun(s1,s2)功能是将字符数组s2中的字符串拷贝到字符数组s1中。拷贝时,'\0'也拷贝过去。
时间: 2024-05-04 11:22:26 浏览: 23
下面是一个示例程序,其中包含了函数表达式 `fun(s1,s2)`,用于将字符数组 `s2` 中的字符串拷贝到字符数组 `s1` 中:
```c
#include <stdio.h>
#include <string.h>
void fun(char s1[], char s2[]) {
int i = 0;
while (s2[i] != '\0') { // 遍历 s2 直到遇到 '\0'
s1[i] = s2[i]; // 将 s2 中的字符拷贝到 s1 中
i++;
}
s1[i] = '\0'; // 在 s1 的末尾加上 '\0'
}
int main() {
char s1[100], s2[100];
printf("请输入字符串 s2:");
scanf("%s", s2);
fun(s1, s2); // 调用函数
printf("拷贝完成后的字符串 s1 为:%s\n", s1);
return 0;
}
```
在这个示例程序中,我们首先定义了一个函数 `fun(s1, s2)`,用于将字符数组 `s2` 中的字符串拷贝到字符数组 `s1` 中。该函数使用了一个循环来遍历字符数组 `s2`,并将其中的字符逐一拷贝到字符数组 `s1` 中。在拷贝完成后,我们需要在字符数组 `s1` 的末尾添加一个结束符 `'\0'`,以表示该字符串的结束。
在 `main` 函数中,我们首先定义了两个字符数组 `s1` 和 `s2`,并通过 `scanf` 函数从标准输入中读取字符数组 `s2`。然后,我们调用函数 `fun(s1, s2)`,将字符数组 `s2` 中的字符串拷贝到字符数组 `s1` 中。最后,我们输出拷贝完成后的字符数组 `s1`。
相关推荐
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)