str类常用函数源代码
来源:互联网 发布:淘宝刷手会被坐牢吗 编辑:程序博客网 时间:2024/06/01 08:50
strlen() 求字符串的长度
源代码:
#include<assert.h>//strlen 源码int strlen(const char *str){ assert(*str != '\0'); //断言,判断字符串是否为空,下同 const char *cp = str; //str依然为字符串首位置 while(*cp ++); return (cp - str - 1); //-1是因为字符串末尾有\0, strlen得到的字符串长度没有\0}
strcmp() 判断两个字符串的大小
源代码:
#include<assert.h>//判断字符串大小int strcmp(const char *src, const char *dst){ assert(NULL != src && NULL != dst); while(*src && *dst && *src++ == *dst++);//当两个字符串都不为空切当前字符串相等时 return *src - *dst; //返回当前不等字符差值,根据返回值的正负号判断大小}
strcpy() 将目标字符串复制给源字符串并覆盖源字符串
源代码:
#include<assert.h>//copy字符串char *strcpy(char *src, const char *dst){ assert(NULL != src && NULL != dst); char *temp = src; //因为下面*str++程序运行完时str不是指向字符串首位置,所以需要定义一个新的字符串保存字符串首地址,下同 while(*src++ = *dst++); return temp;}
strncpy() 按照指定字符个数copy字符串
源代码:
#include<assert.h>//按照指定个数copy字符串char *strncpy(char *src, const char *dst, int count){ assert(NULL != src && NULL!= dst); char *temp = src; while((count--) && (*src++ = *dst++));//这里要注意count--要在&&前面,不然会出错 *src = '\0'; //由于不是copy整个字符串,所以结尾可能不是\0所以要自己加上return temp;
}
strcat() 将两个字符串拼接
源代码:
#include<assert.h>//拼接字符串char *strcat(char *src, const char *dst){ assert(NULL != src && NULL != dst); char *temp = src; while(*src) //让src字符串跑到结尾,然后跟strcpy一样实现拼接 { src ++; } while(*src++ = *dst++); return temp;}
strncat() 按照指定个数实现制定个数字符的拼接
源代码:
#include<assert.h>//按照指定字符个数拼接字符串char *strncat(char *src, const char *dst, int count){ assert(NULL != src && NULL != dst); char *temp = src; while(*src) { src++; } while((count--) && (*src++ = *dst++)); return temp;}
0 0
- str类常用函数源代码
- str 类常用的函数
- [str] -- 常用字符串函数
- c str常用函数原型
- str类函数
- SQL常用函数之五 str()
- VC的几个常用的str函数
- php中常用array内置函数,str内置函数
- [整理]php常用函数及运用场景----字符串函数str
- Str函数
- str()函数
- Python中Str类的常用方法
- python常用内置函数dir,type,str等
- JSTL中的常用EL函数(fn:contains(str,subStr))
- c语言str类函数族
- c语言str类函数族
- python-str(常用方法)
- 模拟实现str类函数和几个内存操作函数
- 编解码基础1
- 深入Android MediaPlayer的使用方法详解
- spring中使用注解启动定时器
- 算法提高 9-1九宫格
- dns 和 ddns
- str类常用函数源代码
- zoj 3614-二维RMQ+容斥
- Android重要的文件目录mnt/sdcard 和 /sdcard
- 为什么IO-multiplexing要与非阻塞IO一起使用
- kafka的安装与配置详解
- 软件错误与软件缺陷
- 历届试题 幸运数
- C++作业5
- Python_获得系统时间