#include <stdio.h>
#include <stdlib.h>
//为避免与标准库中的函数发生混淆,我将它们命名为stringNCopy和stringLength
char *stringNCopy(char *dest, const char *src, size_t count)
{
    for (size_t i = 0; i < count; ++i)
    {
        dest[i] = src[i];
        if (!src[i])
        {
            break;
        }
    }
    return dest;
}
size_t stringLength(const char *str)
{
    size_t count = 0;
    while (*str++)
    {
        ++count;
    }
    return count;
}
int main()
{
    char *dest = malloc(sizeof(char) * 100);
    const char *src = "hello world\n世界你好";
    stringNCopy(dest, src, stringLength(src) + 1);
    printf("%s\n", dest);
    free(dest);
    char arr[50];
    char *ret = stringNCopy(arr, "programming 程序设计", 10000);
    printf("%s\n%s\n", arr, ret);
    ret = stringNCopy(arr, "Programmer", 1);
    printf("%s\n%s\n", arr, ret);
    char test[2];
    const char *str = "测试\0Test";
    ret = stringNCopy(test, str, 100); //非法的操作,可通过编译,但是运行之后可能会报错(vc++下编译并运行之后会报错,Windows下gcc编译后运行无报错)
    printf("%s\n%s\n", test, ret);
    printf("%u\n%u\n%u\n", stringLength(""), stringLength("\0\0\0"), stringLength("字符串 String"));
    return 0;
}
												

strncpy和strlen的可能的实现的更多相关文章

  1. strcpy、strncpy、strlen、memcpy、memset、strcat、strncat、strcmp、strncmp,strchr

    1.strcpy #include<stdio.h> #include<assert.h> char *mystrcpy(char *dest, const char *src ...

  2. Linux进程间通信—共享内存

    五.共享内存(shared memory) 共享内存映射为一段可以被其他进程访问的内存.该共享内存由一个进程所创建,然后其他进程可以挂载到该共享内存中.共享内存是最快的IPC机制,但由于linux本身 ...

  3. http协议中content-length 以及chunked编码分析

    转载请注明出处 http://blog.csdn.net/yankai0219/article/details/8269922 0.序 1.http/1.1协议中与chunked编码的相关字段 1)E ...

  4. 菜鸟 学注册机编写之 “MD5”

    测试环境  系统: xp sp3 调试器 :od 1.10 sc_office_2003_pro 高手不要见笑,仅供小菜玩乐,有不对或不足的地方还请多多指教,不胜感激! 一:定位关键CALL 1. 因 ...

  5. 《嵌入式linux应用程序开发标准教程》笔记——8.进程间通信

    , 8.1 概述 linux里使用较多的进程间通信方式: 管道,pipe和fifo,管道pipe没有实体文件,只能用于具有亲缘关系的进程间通信:有名管道 named pipe,也叫fifo,还允许无亲 ...

  6. gperftools cpp wrapper

    gperftools cpp wrapper // Compile command : ${CXX} -o test_profiler.elf -DUSE_GPERFTOOLS -DDEBUG -D_ ...

  7. CVE-2013-2094 porting to x86-32 分析

    /* * linux 2.6.37-3.8.8 - x86 * @rikiji * * requires System.map and /dev/ptmx * this: http://zmbs.ne ...

  8. 转:Linux--进程间通信(信号量,共享内存)

    源地址:http://www.cnblogs.com/forstudy/archive/2012/03/26/2413724.html Linux--进程间通信(信号量,共享内存)(转)   一. 信 ...

  9. c js 字符串反转

    1.例如:输入 i am     a student    输出 student a am i #include <stdio.h> #include <string.h> v ...

随机推荐

  1. nginx,lvs,haproxy负载均衡对比

    Nginx/LVS/HAProxy是目前使用最广泛的三种负载均衡软件,一般对负载均衡的使用是随着网站规模的提升根据不同的阶段来使用不同的技术,具体的应用需求还得具体分析. 如果是中小型的Web应用,比 ...

  2. Python MySQLdb的execute和executemany的使用

    如果使用executemany对数据进行批量插入的话,要注意一下事项: conn = MySQLdb.connect(host = “localhost”, user = “root”, passwd ...

  3. 使用Apriori算法进行关联分析

    关联分析是一种在大规模数据集中寻找有趣关系的任务.这些关系可以有两种形式:频繁项集或者关联规则.频繁项集是指经常出现在一块的物品的集合,关联规则暗示两种物品之间可能存在很强的关系.一个项集的支持度被定 ...

  4. 【Luogu】P3628特别行动队(斜率优化DP)

    题目链接 设c[i]是战斗力前缀和,f[i]是考虑前i个,且最后一组分到第i个士兵为止的战斗力之和 则有朴素状态转移方程 ;i<=n;++i) ;j<i;++j){ int x=c[i]- ...

  5. python练习之-计算器

    学习以堆栈模式编写-计算器 堆栈特点:先进后出, 如下: #!/opt/python3/bin/python3 # Author: yong import re def is_symbol(eleme ...

  6. BZOJ——1610: [Usaco2008 Feb]Line连线游戏

    http://www.lydsy.com/JudgeOnline/problem.php?id=1610 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 2 ...

  7. 另一篇xtion、kinect选择比较(openni下)

    小小Xtion开箱测评!!2012年03月12日 19:52:55 原文:http://page.renren.com/601107241/note/811764499 ASUS Xtion Pro ...

  8. 数据结构之区间K大数

    求区间的问题有很多类,虽然前人有很多讲解了: 但是我在这里在普及一下,算是自己的一种复习吧. 1.静态询问一个区间的的第k大数,比如询问[l,r] k大数.虽然主席树可以处理,但是这类问题应该是划分树 ...

  9. Ubuntu 16.04安装Mac OS 12虚拟机资源(没成功,但资源还是可以用)

    整理的Mac OS 12虚拟机资源.装虚拟机基本是按这样的套路: 1.先装VM 2.破解VM使其支持Mac OS 12,这个脚本基本是全平台支持,可以看里面的教程文档. 3.用镜像安装系统. 资源: ...

  10. django : related_name and related_query_name

    This post is about two Django ForeignKey parameters related_name related_query_name See an example b ...