C语言strncpy函数实现
来源:互联网 发布:简约官网源码 编辑:程序博客网 时间:2024/05/18 04:30
可见使用位移不但效率高,而且代码书写简洁至极,推荐使用!
#include <stdio.h>char *My_strncpy(char *strDes, const char *strSrc, int n){char *sS = strSrc;char *sD = strDes;int srclen = strlen(strSrc);//为了避免越界计算最终要拷贝多少个字符n = srclen > n ? n : srclen; //方法一,采用下标/*int constN = n;int indexN = n;while (indexN >= 0){if (constN == indexN){*(sD + (constN - 0)) = '\0';}else{*(sD + (indexN)) = *(sS + indexN);printf("%c\n", *(sD + (indexN)));}indexN--;}*///方法二,采用位移 (推荐使用)while (n > 0){*sD = *sS; sS++;sD++;n--;}//将末尾补\0标志字符串结束*sD = '\0';//这里返回值的作用是为了可以进行级联操作//比如: strcat(str, strncpy(buf, src, 10));return strDes;}int main(void){char strD[100];char strS[] = "helloworld";My_strncpy(strD, strS, 5);printf("%s", strD);system("pause");return 0;}
0 0
- C语言strncpy函数实现
- C语言 strncpy函数实现
- 【C语言】模拟实现strcpy()、strncpy()函数
- C语言strncpy()函数
- C语言strncpy()函数
- C语言之strncpy函数
- C语言模拟实现strncpy、strncpy、strncat、strstr和strrstr函数实现
- strcpy/strncpy/strcat/strcmp/strlen函数的c语言实现
- 模拟实现C语言strncpy.strncat.strncmp函数
- 【C语言】模拟实现strncpy、strncat、strncmp函数
- 【C语言】模拟实现strncpy strncat strncmp strchr strrchr函数
- C语言~模拟实现strncpy、strncat、strncmp函数
- C语言之strncpy函数原型
- C语言的strcpy()和strncpy()函数
- 【C语言】 实现strncpy,strncat,strncmp
- C语言模拟实现strncpy、strcat
- C语言中strlen、strcpy、strncpy、strcat、strnact、strcmp、memcpy函数的实现
- C语言中strlen、strcpy、strncpy、strcat、strnact、strcmp、memcpy函数的实现
- 解决spring-security-oauth2 导入包冲突
- 向下一个活动传递数据 --9
- Python核心编程 第九章练习
- UVa455--Periodic Strings
- 3-1 错排
- C语言strncpy函数实现
- 折半插入排序
- JAVA导入三方jar时变异成功但无法运行
- Python与JavaWeb的第一次碰撞
- 子串和
- 论合优米的”互联网+”
- 返回数据给上一个活动 --10
- Android手机之间Socket通信
- Git的使用总结