|
---|
char *strcat( char *strDestination, const char *strSource ); |
一.函数介绍
作用:连接字符串的函数,函数返回指针,两个参数都是指针,第一个参数所指向的内存必须能容纳两个字符串连接后的大小
strSource:源字符串
strDestination:目的字符串
int main()
{
char dest[20] = "hello ";
char *src = "world";
strcat(dest, src);
printf("%s\n", dest);
system("pause");
return 0;
}
二. 函数实现
1.先将目的字符串遍历完,指针指向字符串最后面
2.将源字符串赋值给目的字符串
3.打印目的字符串
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<assert.h>
#include<string.h>
#include<stdlib.h>
char *MyStrcat(char *dest, const char *src)
{
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 = "world";
MyStrcat(dest, src);
printf("%s\n", dest);
system("pause");
return 0;
}
打印结果
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)