当前位置:网站首页>C语言-6月10日-my_strcat函数的编写
C语言-6月10日-my_strcat函数的编写
2022-08-11 05:30:00 【曾铎000811】
my_strcat_array(char *str,char*arr);形式参数自定义,将array1、array2中的数据连接到str数组中:array1:"hello" array2:"world" -> str:helloworld
//my_strcat_array(char *str,char*arr);形式参数自定义,将arr中的数据连接到str数组中:"hello""world" -> str:helloworld
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
void my_strcat_array(char *str,char *array1,char *array2)//定义三个数组,array1用来存放hello,array2用来存放world,str作为大数组用来连接两个数组
{
assert(str != NULL && array1 != NULL && array2 != NULL);
for(int i = 0;i < strlen(array1);++i){
str[i] = array1[i];
}
for(int i = 0;i < strlen(array2);++i){
str[strlen(array1) + i] = array2[i];
}
}
int main()
{
char array1[] = "hello";
char array2[] = "world";
char str[5];
my_strcat_array(str,array1,array2);
printf("%s ",str);
return 0;
}
如图为运行结果:

输出完成
如图所示,程序成功连接两个数组,存放进了str数组中并输出。
使用字符串头文件更改函数的代码为:
void my_strcat_array(char *str,char *array)
{
assert(str != NULL && array != NULL);
int len = strlen(str);
for(int i = 0;array[i] != '\0';i++){
str[len + i] = array[i];
}
}经过优化,此方法只需要开辟两个数组,相比较上面的代码简便了不少。
如图所示为程序运行结果:

输出完成
利用指针:
#include<stdio.h>
#include<iostream>
#include<assert.h>
void my_strcat(char *message1,char *message2)
{
assert(message1 != nullptr && message2 != nullptr);
char *p = message1;
char *q = message2;
assert(p != nullptr && q !=nullptr);
while(*p){//此循环存在的原因:当p指针还停留在第一个字符串时,一直向后迁移,直到到达第一个字符串的末尾
p++;
}
while(*q != '\0'){//跳出第一个循环之后,当指针没有到达第二个字符串的末尾时
*p++ = *q++;//将第二个字符串的字符添加在第一个字符串的后面,循环。
}
}
int main()
{
char message1[100] = {0};
char message2[100] = {0};
printf("Please input your first string: \n");
scanf("%s",message1);
printf("Please input your second string: \n");
scanf("%s",message2);
my_strcat(message1,message2);
printf("The string you connected is %s\n",message1);
return 0;
}如图我输入hello和world,运行结果为:
连接字符串成功
边栏推荐
- SearchGuard configuration
- js 学习进阶(事件高级 pink老师教学笔记)
- [Meetup] OpenMLDBxDolphinScheduler engineering and scheduling link link characteristics, building the end-to-end MLOps workflow
- 虚拟机更改IP地址
- 厂商推送平台-华为接入
- Intelligent risk control China design and fall to the ground
- OpenMLDB官网升级,神秘贡献者地图带你快速进阶
- 自己动手写RISC-V的C编译器-00环境配置
- 自己动手写RISC-V的C编译器-01实现加减法
- Tinker接入全流程---配置篇
猜你喜欢

IndexError: index 9 is out of bounds for axis 0 with size 9;数组下标溢出问题

C语言-内存操作函数

Tinker's self-introduction

【无标题】

贡献者任务第三期精彩来袭

jdbc接口文档参考,jdbc接口方法逻辑探究

Real-time Feature Computing Platform Architecture Methodology and Practice Based on OpenMLDB

Tinker接入全流程---配置篇

C语言预处理
![[Meetup]OpenMLDBxDolphinScheduler 链接特征工程与调度环节,打造端到端MLOps工作流](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[Meetup]OpenMLDBxDolphinScheduler 链接特征工程与调度环节,打造端到端MLOps工作流
随机推荐
C-自定义类型(结构体、枚举、联合)
127.0.0.1 已拒绝连接
PyQt5中调用.ui转换的.py文件代码解释
2022DASCTF X SU 三月春季挑战赛 checkin ROPgadget进阶使用
星盟-pwn-fog
Byte (byte) and bit (bit)
Day 80
解决AttributeError: ‘NoneType‘ object has no attribute ‘val‘ if left.val!=right.val:Line 17 问题
C语言实现简易扫雷(附带源码)
品优购项目实战笔记
nepctf Nyan Cat 彩虹猫
自己动手写RISC-V的C编译器-02语法描述方法和递归下降解析
PAT乙级刷题之路
解决npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead.
第一章 Verilog语言和Vivado初步使用
Certificate of SearchGuard configuration
Day 71
轻松理解进程与线程
智能风控中台设计与落地
Here is a memorial