当前位置:网站首页>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,运行结果为:
连接字符串成功
边栏推荐
- Manufacturer Push Platform-Huawei Access
- 一文看懂注解与反射
- helm安装
- mysql basic summary
- The third phase of the contributor task is wonderful
- OpenMLDB Pulsar Connector: Efficiently connect real-time data to feature engineering
- Asis2016 books null off by one
- [Meetup Preview] OpenMLDB+OneFlow: Link feature engineering to model training to accelerate machine learning model development
- JVM tuning and finishing
- 无效的修订:3.18.1-g262b901-dirty
猜你喜欢
轻松理解进程与线程
星盟-pwn-fog
jdbc接口文档参考,jdbc接口方法逻辑探究
Jetpack使用异常问题集锦
OpenMLDB官网升级,神秘贡献者地图带你快速进阶
Intelligent risk control China design and fall to the ground
OpenMLDB v0.5.0 发布 | 性能、成本、灵活性再攀高峰
Visual studio2019 configuration uses pthread
IndexError: index 9 is out of bounds for axis 0 with size 9;数组下标溢出问题
JVM tuning and finishing
随机推荐
Building a data ecology for feature engineering - Embrace the open source ecology, OpenMLDB fully opens up the MLOps ecological tool chain
Day 70
three.js基础学习
Day 80
Use c language to implement tic-tac-toe chess (with source code, you can run it directly)
The whole process of Tinker access --- configuration
mk file introduction
JS case exercise (classic case of teacher pink)
js learning advanced (event senior pink teacher teaching notes)
C语言-内存操作函数
Here is a memorial
Thesis unscramble TransFG: A Transformer Architecture for Fine - grained Recognition
Day 86
自己动手写RISC-V的C编译器-00环境配置
Goldbach's conjecture and the ring of integers
活动预告 | 4月23日,多场OpenMLDB精彩分享来袭,不负周末好时光
Tinker's self-introduction
8-byte standard request parsing during USB enumeration
Some formulas for system performance and concurrency
The role of the port