当前位置:网站首页>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,运行结果为:
连接字符串成功
边栏推荐
- c语言-数据存储部分
- OpenMLDB + Jupyter Notebook:快速搭建机器学习应用
- OpenMLDB: Consistent production-level feature computing platform online and offline
- JS事件循环机制
- C-自定义类型(结构体、枚举、联合)
- mysql basic summary
- 解决AttributeError: ‘NoneType‘ object has no attribute ‘val‘ if left.val!=right.val:Line 17 问题
- C语言预处理
- 杀死进程-查看防火墙状态
- SearchGuard configuration
猜你喜欢
随机推荐
Intelligent risk control China design and fall to the ground
父子节点数据格式不一致的树状列表实现
活动预告 | 4月23日,多场OpenMLDB精彩分享来袭,不负周末好时光
js learning advanced (event senior pink teacher teaching notes)
欧拉法解微分方程
swagger错误:WARN i.s.m.p.AbstractSerializableParameter - [getExample,421] - Illegal DefaultValue null
一文看懂注解与反射
Interpretation of the paper: Cross-Modality Fusion Transformer for Multispectral Object Detection
Some formulas for system performance and concurrency
OpenMLDB v0.5.0 发布 | 性能、成本、灵活性再攀高峰
Day 80
JNI入门
开源机器学习数据库OpenMLDB贡献者计划全面启动
The official website of OpenMLDB is upgraded, and the mysterious contributor map will take you to advance quickly
C语言实现猜数字(附带源码,可直接运行)
OpenMLDB官网升级,神秘贡献者地图带你快速进阶
SearchGuard configuration
Certificate of SearchGuard configuration
OpenMLDB: Consistent production-level feature computing platform online and offline
Pinyougou project combat notes