c语言实现strcat函数

char *strcat( char *strDestination, const char *strSource );
一.函数介绍
作用:连接字符串的函数,函数返回指针,两个参数都是指针,第一个参数所指向的内存必须能容纳两个字符串连接后的大小
strSource:源字符串
strDestination:目的字符串
int main() { char dest[20] = "hello "; char *src = "https://www.it610.com/article/world"; strcat(dest, src); printf("%s\n", dest); system("pause"); return 0; }

【c语言实现strcat函数】二. 函数实现
1.先将目的字符串遍历完,指针指向字符串最后面
2.将源字符串赋值给目的字符串
3.打印目的字符串
#define _CRT_SECURE_NO_WARNINGS #include.h> #include #include.h> #include.h> char *MyStrcat(char *dest, const char *src) //将源字符串加const,防止其内部被改变,表明其为输入参数 { char *p = dest; assert((dest != NULL) && (src != NULL)); //对源地址和目的地址进行断言 while (*dest != '\0')//先遍历完目的字符串,指针指向目的字符串最后面 { dest++; } while ((*dest++ = *src++) != '\0')//将源字符串赋给目的字符串 { ; } return p; //为实现链式操作,将目的地址返回 } int main() { char dest[20] = "hello "; char *src = "https://www.it610.com/article/world"; MyStrcat(dest, src); printf("%s\n", dest); system("pause"); return 0; }

打印结果
c语言实现strcat函数
文章图片

    推荐阅读