C语言模拟实现strncpy、strcat
2017-01-09 21:36
543 查看
模拟实现strncpy
先演示一下strncpy的效果:
代码如下:
运行结果:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202006/01/4bbc243848a8e5b6567b34b4faf96483)
通过以上演示可以看出,strncpy实现的是用一个固定的字符串去替换原字符串中的某段字符,可以设置一个循环,来实现字符的替换,如果它只有一个字符要替换,则循环执行一次,设置一个循环判断语句即可,循环次数等于要替换的字符数。
程序代码如下:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202006/01/87ad1738b9d0d8ec8327b76f17d61348)
模拟实现strcat,程序代码如下:
运行结果如图:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202006/01/4b55d5a7ef1f1a8208284c7035b51358)
实现程序功能方法有多种,不一定非得要死板的模仿哪一种方法,而是自己慢慢地去探索。
先演示一下strncpy的效果:
代码如下:
#include <string.h> #include <stdio.h> void main(void) { char string[100] = "Cats are nice usually"; printf("Before: %s\n", string); strncpy(string, "Dogs", 4); strncpy(string + 9, "funn", 4); printf("After: %s\n", string); system("pause"); }
运行结果:
通过以上演示可以看出,strncpy实现的是用一个固定的字符串去替换原字符串中的某段字符,可以设置一个循环,来实现字符的替换,如果它只有一个字符要替换,则循环执行一次,设置一个循环判断语句即可,循环次数等于要替换的字符数。
程序代码如下:
#include <Windows.h> #include <string.h> #include <stdio.h> #include <assert.h> char *mystrncpy(char *strDest, const char *strSource, size_t count) { assert(strDest); assert(strSource); while (count--) { *strDest++ = *strSource++; } return strDest; } void main(void) { char string[100] = "Cats are nice usually"; printf("Before: %s\n", string); mystrncpy(string, "Dogs", 4); mystrncpy(string + 9, "hola", 4); printf("After: %s\n", string); system("pause"); return 0; }
模拟实现strcat,程序代码如下:
#include <Windows.h> #include <string.h> #include <stdio.h> #include <assert.h> char *mystrcat(char *strDest, const char *strSource) { int len = strlen(strDest); assert(strDest); assert(strSource); char *ret = strDest; char *newstrDest = strDest + len; //新接的字符串开始位置 while (*strSource) { *newstrDest++ = *strSource++; //把要加的字符串一个一个copy过去 } return ret; } int main() { char string[120]; strcpy(string, "Hello world from "); strcat(string, "strcpy "); strcat(string, "and "); strcat(string, "strcat haha!"); printf("String = %s\n", string); system("pause"); return 0; }
运行结果如图:
实现程序功能方法有多种,不一定非得要死板的模仿哪一种方法,而是自己慢慢地去探索。
相关文章推荐
- C语言模拟实现strcpy、strncpy、strcmp、strcat、strstr
- [C语言]模拟实现strcpy/strncpy/strcat/strncat/strcmp/strncmp/strstr
- 模拟实现C语言strncpy.strncat.strncmp函数
- 【C语言】【面试题】【笔试题】模拟实现strncpy
- 【c语言】模拟实现strcat()、strncat()函数
- 【面试题】C语言:模拟实现strncpy,尝试比较strncpy与strcpy区别。
- 模拟实现strcmp,strncmp,strstr,strcat,strncat,strcpy,strncpy,strlen ,memcpy,memmove等函数
- c语言模拟实现strcat
- 模拟实现//strcpy //strncpy //strcat //strncat //strcmp //strncmp //memcpy //memmove
- C语言字符串函数的模拟实现(strlen、strcpy、strcat、strstr 、strcmp)
- 用c语言.模拟实现strcpy,strcat,strcat,memcpy,memmove
- 【C语言】模拟实现strcpy、strcat、strcmp函数
- 【笔试题】C语言:模拟实现strcat
- 模拟实现strcpy,strncpy,strcat,strncat,strcmp,strncmp
- 【笔试题】C语言:模拟实现strcat
- 模拟实现C语言strncpy.strncat.strncmp函数
- C语言中strlen、strcpy、strncpy、strcat、strnact、strcmp、memcpy函数的实现
- 模拟实现strcpy 模拟实现strncpy 模拟实现strcat 模拟实现strncat 模拟实现strcmp 模拟实现strncmp 模拟实现memcpy 模拟实现memmove
- 模拟实现strlen、strcpy、strncpy、strcat、strcmp
- c语言中有关字符串函数strcpy,strcmp,strcat,strstr的模拟实现