C标准库string.h源码(简单版)

2023-05-16

C标准库string.h源码(简单版)

  • strcpy/strncpy
  • strcat/strncat
  • strcmp/strncmp
  • strchr/strrchr/strchrnul/strstr
  • memcpy/memmove

strcpy/strncpy

char * strcpy (char * dst, const char * src)
{
    char * cp = dst;
    while( *cp++ = *src++ != '\0')
        ;               /* Copy src over dst */
    return( dst );
}
/*​strncpy函数:从src复制n个字符到dest中,若n<strlen(src),则dest不以'\0'结尾,
  若n>strlen(src),则多余的字节被填充'\0',算法如下
Purpose:
       Copies count characters from the source string to the
       destination.  If count is less than the length of source,
       NO NULL CHARACTER is put onto the end of the copied string.
       If count is greater than the length of sources, dest is padded
       with null characters to length count.
*/
char * strncpy (char *dest, const char *source, unsigned count)
{
    char *start = dest;
 
    while (count && (*dest++ = *source++ != '\0'))
        count--;
 
    if (count)
        while (--count)
            *dest++ = '\0';
 
    return(start);
}

strcat/strncat

/*
char *strcat(dst, src) - concatenate (append) one string to another
Purpose:
       Concatenates src onto the end of dest.  Assumes enough
       space in dest.
*/
 
char * strcat (char * dst, const char * src)
{
    char * cp = dst;
 
    while( *cp )
        ++cp;           /* Find end of dst */
    while( *cp++ = *src++ )
       ;               /* Copy src to end of dst */
    return( dst );
}
/*
char *strncat(front, back, count) - append count chars of back onto front
Purpose:
       Appends at most count characters of the string back onto the
       end of front, and ALWAYS terminates with a null character.
       If count is greater than the length of back, the length of back
       is used instead.  (Unlike strncpy, this routine does not pad out
       to count characters).
*/
 
char *strncat (char *front, const char *back, unsigned count)
{
    char *start = front;
 
    while (*front++)
        ;
    front--;
 
    while (count--)
        if ((*front++ = *back++) == '\0')
            return(start);
    *front = '\0';
    return(start);

strcmp/strncmp

/*
strcmp - compare two strings, returning less than, equal to, or greater than
Purpose:
       Compares two string, determining their lexical order.  Unsigned
       comparison is used.return < 0, 0, or >0, indicating whether the 
       first string is Less than, Equal to, or Greater than the second string.
*/
int strcmp ( const char *s1, const char *s2)
{
    for(;(*s1 == *s2) && (*s1 != '\0');s1++,s2++)
        ;
    return *s1-*s2;
}
/*int strncmp(first, last, count) - compare first count chars of strings
Purpose:
       Compares two strings for lexical order.  The comparison stops
       after: (1) a difference between the strings is found, (2) the end
       of the strings is reached, or (3) count characters have been compared.
*/
int strncmp(const char *s1, const char *s2, size_t n)
{
 
    for(;(*s1 != '\0') && (count > 0) && (*s1 == *s2);s1++,s2++,count--)
        ;
    if(count == 0)
        return 0;
    else
        return *s1 - *s2;
}

strchr/strrchr/strchrnul/strstr

char *strchr(const char *s, int c);
返回一个字符指针,它指向c在s中首次出现的位置,如果未找到,返回NULL

char *strrchr(const char *s, int c);
返回一个字符指针,它指向c在s中最后出现的位置,如果未找到,返回NULL

char *strchrnul(const char *s, int c);
若c在s中,同strchr,否则返回指向字符串末尾的‘\0’的指针,而不是NULL

char *strstr(const char *str, const char *sub);
查找子串strstr:在字符串str中查找子串sub,若找到,则指针返回sub第一次出现在str中的位置,否则返回NULL


/*
char *strchr(string, chr) - search a string for a character
Purpose:
       Searches a string for a given character, which may be the
       null character '\0'.
*/
char *strchr (const char *string, int chr)
{
    while (*string && *string != chr)
        string++;
    if (*string == chr)
        return(string);
    return((char *)0);
}
/*
       returns a pointer to the last occurrence of ch in the given
       string. returns NULL if ch does not occurr in the string
*/
 
char *strrchr (const char *string, int ch)
{
    char *start = string;
    while (*string++)
        ;
    while (--string != start && *string != ch)
        ;
    if (*string == ch)
        return(string);
    return(NULL);
}
//若c在s中,同strchr,否则返回指向字符串末尾的‘\0’的指针,而不是NULL
char *strchrnul (const char *string, int chr)
{
    while (*string && *string != chr)
        string++;
    return(string); 
}

/***
*char *strstr(string1, string2) - search for string2 in string1
*
*Purpose:
*       finds the first occurrence of string2 in string1
*
*Entry:
*       char *string1 - string to search in
*       char *string2 - string to search for
*
*Exit:
*       returns a pointer to the first occurrence of string2 in
*       string1, or NULL if string2 does not occur in string1
*******************************************************************************/
char * strstr (const char * str1,const char * str2)
{
        char *cp = (char *) str1;
        char *s1, *s2;
 
        if ( *str2 == '\0' )
            return((char *)str1);
 
        while (*cp)
        {
                s1 = cp;
                s2 = (char *) str2;
 
                while ( *s1 && *s2 && (*s1 == *s2) )
                        s1++, s2++;
 
                if (*s2 == '\0')
                        return(cp);
 
                cp++;
        }
 
        return(NULL);
}

memcpy/memmove

void* memcpy(void* memTo, void* memFrom, size_t size)
{
	assert(memTo != NULL && memFrom != NULL);
	char* temFrom = (char*)memFrom;
	char* temTo = (char*)memTo;
	while(size-- > 0)
		*temTo++ = *temFrom++;
	return memTo;
}

参考:https://blog.csdn.net/c1204611687/category_6985722.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C标准库string.h源码(简单版) 的相关文章

  • 树莓派4b 安装buster系统 安装ROS与MAVROS 连接pixhawk飞控

    手把手教程 无屏幕的树莓派4b 安装Rapbian buster系统 安装ROS与MAVROS 连接pixhawk飞控 xff08 1 xff09 准备 xff1a 树莓派4b 1 32G内存卡 1 树莓派官方raspbian buster
  • 阿克曼前轮转向车gazebo模型

    想要一个阿克曼转向结构车的gazebo模型 xff0c 要求能够用ros话题控制前进速度和前轮转角 令人惊讶的是 xff0c 网上基本没有这种模型 racecar模型 首先古月居提供了一个racecar的模型 xff0c 可以控制速度和前轮
  • Jetson TX2 在docker容器中import torch 报错的处理方式

    1 Jetson TX2 信息 xff1a 驱动版本 xff1a JetPack 4 6 1 2 docker信息 xff1a docker 镜像 xff1a pull 了 nvcr io nvidia l4t ml r32 7 1 py3
  • 史上最详细的PID教程——理解PID原理及优化算法

    Matlab动态PID仿真及PID知识梳理 云社区 华为云 huaweicloud com 位置式PID与增量式PID区别浅析 Z小旋 CSDN博客 增量式pid https zhuanlan zhihu com p 38337248 期望
  • JAVA经典试卷(理工)

    一 判断题 xff08 本大题共20小题 xff0c 每小题1分 xff0c 总计20分 xff09 1 xff0e final类能派生子类 2 xff0e 子类要调用父类的方法 xff0c 必须使用super关键字 3 xff0e Jav
  • git与gitee学习笔记

    随着时间推移 xff0c 除去常量 xff0c 任何事物都是在变化的 xff0c 如果用一根曲线表示 xff0c 横轴代表时间 xff0c 纵轴代表事物量 xff0c 那么所绘制的曲线 xff0c 在时间足够长的情况下 xff0c 必然是高
  • docker基础命令操作---镜像操作

    1 搜索官方仓库镜像 xff1a docker search image name 镜像名 例如 xff1a docker search nginx 命令执行结果参数说明 xff1a 参数 说明 NAME 镜像名称 DESCRIPTION
  • ESP8266连接天猫精灵(一)

    背景 接触天猫精灵后 xff0c 就想作一些小东西能接入天猫精灵 查看官网的文档后 xff0c 选择了ESP系列 xff0c 官方在文档中也比较推荐 读技术文档是个很难受的事情 xff0c 容易犯困 xff0c 最好有可以操作的设备 准备如
  • Windows下Boost库的安装与使用

    目录 1 基本介绍 2 下载安装 3 配置boost环境 xff08 VS2010 xff09 4 测试 1 基本介绍 Boost库是为C 43 43 语言标准库提供扩展的一些C 43 43 程序库的总称 xff0c 由Boost社区组织开
  • 嵌入式JetSon TX2上使用RealSense D435 (外加IMU芯片) 运行RTAB-Map与VINS-MONO的全流程记录

    本周成功的在JetSon TX2上移植了Vins Mono与RTAB Map xff0c 并使用摄像头RealSense D435顺利跑通了这两个框架 中间遇到了各种各样神奇的问题 xff0c 踩坑无数 xff0c 现整理记录一下整体流程
  • 微信公众号本地开发调试 - 无公网IP,内网穿透

    文章目录 前言1 配置本地服务器2 内网穿透2 1 下载安装cpolar内网穿透2 2 创建隧道 3 测试公网访问4 固定域名4 1 保留一个二级子域名4 2 配置二级子域名 5 使用固定二级子域名进行微信开发 前言 在微信公众号开发中 x
  • opencv图像通道 8UC1?

    转载自博主 64 马卫飞 https blog csdn net maweifei article details 51221259 CV lt bit depth gt S U F C lt number of channels gt b
  • gazebo中urdf、xacro、sdf模型文件关系

    gazebo的模型是用xml格式的文本文件来描述的 具体有三种形式 xff1a urdf xacro sdf urdf urdf是老的gazebo模型格式 xff0c 本身有一些缺陷 xff0c 也缺一些功能 但是网上很多gazebo模型都
  • 1_树莓派开启ssh服务

    树莓派3 开启 SSH 服务 原文链接 xff1a https blog csdn net qq 16775293 article details 88385393 文章目录 1 使用管理工具2 启动服务3 自动启动服务 3 1 Windo
  • 树莓派4b串口通信配置

    树莓派4b本身是两个串口 xff0c 运行ls dev al如下 xff1a 请注意 xff1a 在默认状态下 xff0c serial0 就是GPIO14 15 是映射到ttyS0的 xff08 就是MINI串口 xff1a dev tt
  • Pandas第三次作业20200907

    练习1 读取北向 csv 指定trade date为行索引 查看数据的基本信息 有无缺失值 对其缺失值进行处理 删除缺失值所在行 查看数据的基本信息 查看数据是否清洗完毕 index列没啥用 将index列删除 观察数据是否有重复行 将重复
  • 新手入门板卡硬件调试

    硬件电路调试步骤 新手入门板卡硬件调试一看 观察焊接情况二测 测量阻抗三接触式上电调试遇到的问题一般解决思路电源供电运放出现震荡测量时GND的选取振铃现象 新手入门板卡硬件调试 一看 观察焊接情况 1 拿到板卡后 xff0c 首先观察下焊接
  • 用shell 命令获取占用cpu 最多的前五位

    通常情况下使用ps axu 来获得系统中所有进程占用资源情况 xff0c 通常也可以使用top 命令来动态的获得系统中资源占用最多的进程 假设我们使用ps aux gt file tmp来获取linux系统中的进程占用资源情况 xff0c
  • 关于准确率accuracy和召回率recall的理解

    假设有100个样本 xff0c 其中正样本70 xff0c 负样本30 xff0c 这个是由数据集本身决定的 xff0c 机器要做的就是判别这100个样本中哪几个样本是正样本 xff0c 哪几个样本是负样本 现在机器做出了预测 xff1a
  • pytorch BERT文本分类保姆级教学

    pytorch BERT文本分类保姆级教学 本文主要依赖的工具为huggingface的transformers xff0c 更详细的解释可以查阅文档 定义模型 模型定义主要是tokenizer config和model的定义 xff0c

随机推荐

  • class balanced loss pytorch 实现

    cb loss pytorch 实现 xff0c 可直接调用 参考 xff1a https github com vandit15 Class balanced loss pytorch blob master class balanced
  • 解决不平衡数据集问题

    解决不平衡数据集问题 数据不平衡通常反映数据集中类的不均匀分布 例如 xff0c 在信用卡欺诈检测数据集中 xff0c 大多数信用卡交易都不是欺诈 xff0c 只有很少的类是欺诈交易 这使得我们在欺诈类和非欺诈类之间的比例约为50 1 迄今
  • matlab报错解决---当前文件夹或MATLAB路径中未发现xxxx.m,但它位于xx\xxx\xxx\路径下

    解决 xff1a 选定为找到的文件 xff0c 右键 xff0c 找到 选择文件夹和子文件夹 选项 xff0c 添加到路径即可 xff0c 之后文件会变亮色 xff0c 不是灰色 修改之后 xff0c 发现没有 添加路径 的选项了 最后解决
  • sdf模型插入gazebo_ros_control插件

    gazebo ros control目前只支持老版的urdf模型 xff0c 官方教程 xff1a http gazebosim org tutorials tut 61 ros control sdf模型怎么办呢 xff1f 回答 xff
  • PreparedStatement 在mysql下中文乱码解决方案

    在顶目中无意中碰到PreparedStatement 在存DB时出现乱码 xff0c 困扰了好久终于解决问题 问题代码如下 pstmt 61 con prepareStatement INSERT OFFLINE pstmt setStri
  • 2013年终总结

    2013年即将过去 xff0c 回顾这一年 xff0c 有得有失 xff0c 有喜有悲 xff0c 些许记忆碎片留在脑海中 简单做个总结 xff0c 也算划上一个完美的句号 xff0c 再迎接充满挑战的2014 xff01 项目 一年过来
  • 程序员的生活,其实苦不堪言

    前一天 A 下班前把这个代码发给我 B 好的 xff01 第二天 A 都他妈中午了 xff0c 代码怎么还没发过来 xff1f B 我他妈还没下班呢 xff01 程序猿的真实写照 曾经刚参加工作 xff0c 接手一个项目的维护 xff0c
  • 不容错过的用户标签全面解读。建议收藏!

    过去几年来 xff0c 随着我国整体人口红利优势不再 xff0c 市场竞争加剧 xff0c 获客成本不断飙升 xff0c 互联网也告别增长进入存量时代 xff0c 品牌方的营销目标也从最大化追求用户数量规模转变为追求用户质量的精细化营销上
  • 【书写makefile】相关符号介绍

    本文将介绍一下几种符号 xff1a 61 43 61 61 61 makefile中 xff0c 的意思是取变量的意思 xff0c 比如 xff0c a 61 4 那么在后面的语句中 xff0c a 就代表的是取a的值 如果给a定义的是个宏
  • python人工智能技术

    人工智能 xff08 AI xff09 已成为当今世界的热门话题 xff0c 它的应用范围越来越广泛 其中 xff0c Python成为AI开发中最受欢迎的编程语言之一 Python提供了许多功能强大的库和框架 xff0c 大大简化了开发人
  • 利用X-CTU软件给P900数传配置参数

    转自 xff1a 70条消息 P900数传参数配置 落体偏东 CSDN博客 ATS104设置网络号 xff08 设置主从之间通讯连接的密码 xff09 ATS105设置单元号 xff08 给自己使用的数传进行编号 xff0c 防止主从混乱
  • px4添加自己编写的代码并编译

    1 在px4项目下的src文件夹下的modules文件夹中创建一个文件夹 xff0c 如图我创建了一个position control文件夹 xff0c 在该文件夹中添加自己写的代码程序 xff0c 同时添加一个CmakeLists txt
  • 思岚A1M8激光雷达-ubuntu18.04-slam建图参考

    Rplidar A1 A2使用及Hector SLAM建图 NouriXiiX的博客 CSDN博客 激光雷达初体验 Ubuntu 18 04 43 思岚科技 RPLIDAR A1M8 43 ROS 上手使用 银时大魔王的博客 CSDN博客
  • intel Realsense D/T系列 kalibr标定

    kalibr官方源码GitHub ethz asl kalibr The Kalibr visual inertial calibration toolbox 鼠标下拉找到install follow the install wiki pa
  • gazebo仿真遇到的FCU问题

    当使用roslaunch xxx launch命令进行gazebo仿真时出现 FCU Preflight Fail Accel 0 uncalibrated或者FCU Preflight Fail Baro Sensor 0 missing
  • 大广角USB摄像头选用指南

    起因是我要做一个二维码引导无人机降落的实验 四旋翼无人机搭载单目下视摄像头 xff0c 用于识别地面的二维码 我选择摄像头的标准基本上只有一个 xff1a 视场角越大越好 为此查阅了一些资料 xff0c 买了很多镜头和底板 xff0c 有了
  • 一:XTDrone平台上将视觉SLAM2与gazebo仿真集合

    1 XTDrone仿真平台配置 参考官方教程 xff0c 基本没大问题 仿真平台基础配置 语雀 依赖安装sudo apt install y n https www yuque com xtdrone manual cn basic con
  • 线程和进程的区别

    不少刚看到这两个词 xff08 特别是不是计算机专业的 xff09 小伙伴可能会比较疑惑 xff0c 线程和进程有什么区别 xff0c 网上有许多专业性的解答 xff0c 但是既然不少小伙伴不是计算机专业的 xff0c 那就结合例子做个大概
  • Pixhawk烧写自己开发过的1.11.0固件连接不上QGC

    最近在更改代码烧写固件后 xff0c 飞控就连接不上地面站 xff0c 以为是飞控坏了 xff0c 烧写了最新版的固件发现有可以连接到地面站了 xff0c 又烧写了同一版本的其他代码发现也可以连接qgc xff0c 应该是自己写的代码某个部
  • C标准库string.h源码(简单版)

    C标准库string h源码 xff08 简单版 xff09 strcpy strncpystrcat strncatstrcmp strncmpstrchr strrchr strchrnul strstrmemcpy memmove s