string函数分析

时间:2022-08-23 19:17:48

string函数分析
string函数包含在string.c文件中,经常被C文件使用。
1. strcpy
函数原型: char* strcpy(char* str1,char* str2);
函数功能: 把str2指向的字符串拷贝到str1中去
函数返回: 返回str1,即指向str1的指针
/**
* strcpy - Copy a %NUL terminated string
* @dest: Where to copy the string to
* @src: Where to copy the string from
*/
char * strcpy(char *dest, const char *src)
{
char *tmp = dest;

while((*dest++ = *src++) != ‘\0’);

return tmp;
}
2. strncpy
/**
* strncpy - Copy a length-limited, %NUL-terminated string
* @dest: Where to copy the string to
* @src: Where to copy the string from
* @count: The maximum number of bytes to copy
*
* Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
* However, the result is not %NUL-terminated if the source exceeds
* @count bytes.
*/
char * strncpy(char * dest,const char *src,size_t count)
{
char *tmp = dest;

while (count-- && (*dest++ = *src++) != '\0')
/* nothing */;

return tmp;
}
3. strcat
函数原型: char* strcat(char * str1,char * str2);
函数功能: 把字符串str2接到str1后面,str1最后的'\0'被取消
函数返回: str1
/**
* strcat - Append one %NUL-terminated string to another
* @dest: The string to be appended to
* @src: The string to append to it
*/
char * strcat(char * dest, const char * src)
{
char *tmp = dest;

while (*dest)
dest++;
while ((*dest++ = *src++) != '\0')
;

return tmp;
}
4. strncat
/**
* strncat - Append a length-limited, %NUL-terminated string to another
* @dest: The string to be appended to
* @src: The string to append to it
* @count: The maximum numbers of bytes to copy
*
* Note that in contrast to strncpy, strncat ensures the result is
* terminated.
*/
char * strncat(char *dest, const char *src, size_t count)
{
char *tmp = dest;

if (count) {
while (*dest)
dest++;
while ((*dest++ = *src++)) {
if (--count == 0) {
*dest = '\0';
break;
}
}
}

return tmp;
}
5. strcmp
匹配返回0,不匹配返回非0。
函数原型: int strcmp(char * str1,char * str2);
函数功能: 比较两个字符串str1,str2.
函数返回: str1<str2,返回负数; str1=str2,返回 0; str1>str2,返回正数.
/**
* strcmp - Compare two strings
* @cs: One string
* @ct: Another string
*/
int strcmp(const char * cs,const char * ct)
{
register signed char __res;

while (1) {
if ((__res = *cs - *ct++) != 0 || !*cs++)
break;
}

return __res;
}
6. strncmp
匹配返回0,不匹配返回非0。
/**
* strncmp - Compare two length-limited strings
* @cs: One string
* @ct: Another string
* @count: The maximum number of bytes to compare
*/
int strncmp(const char * cs,const char * ct,size_t count)
{
register signed char __res = 0;

while (count) {
if ((__res = *cs - *ct++) != 0 || !*cs++)
break;
count--;
}

return __res;
}
7. strchr
函数原型: char* strchr(char* str,char ch);
函数功能: 找出str指向的字符串中第一次出现字符ch的位置
函数返回: 返回指向该位置的指针,如找不到,则返回空指针
参数说明: str-待搜索的字符串,ch-查找的字符
/**
* strchr - Find the first occurrence of a character in a string
* @s: The string to be searched
* @c: The character to search for
*/
char * strchr(const char * s, int c)
{
for(; *s != (char) c; ++s)
if (*s == '\0')
return NULL;
return (char *) s;
}
8. strrchr
/**
* strrchr - Find the last occurrence of a character in a string
* @s: The string to be searched
* @c: The character to search for
*/
char * strrchr(const char * s, int c)
{
const char *p = s + strlen(s);
do {
if (*p == (char)c)
return (char *)p;
} while (--p >= s);
return NULL;
}
9. strlen
函数原型: unsigned int strlen(char * str);
函数功能: 统计字符串str中字符的个数(不包括终止符'\0')
函数返回: 返回字符串的长度.
/**
* strlen - Find the length of a string
* @s: The string to be sized
*/
size_t strlen(const char * s)
{
const char *sc;

for (sc = s; *sc != '\0'; ++sc)
/* nothing */;
return sc - s;
}
10. strnlen
/**
* strnlen - Find the length of a length-limited string
* @s: The string to be sized
* @count: The maximum number of bytes to search
*/
size_t strnlen(const char * s, size_t count)
{
const char *sc;

for (sc = s; count-- && *sc != '\0'; ++sc)
/* nothing */;
return sc - s;
}

11. memset
函数原型: void *memset(void *s, int c, size_t n)
函数功能: 字符串中的n个字节内容设置为c
函数返回:
参数说明: s-要设置的字符串,c-设置的内容,n-长度
所属文件: <string.h>,<mem.h>
/**
* memset - Fill a region of memory with the given value
* @s: Pointer to the start of the area.
* @c: The byte to fill the area with
* @count: The size of the area.
*
* Do not use memset() to access IO space, use memset_io() instead.
*/
void * memset(void * s,int c,size_t count)
{
char *xs = (char *) s;

while (count--)
*xs++ = c;

return s;
}
12. bcopy
/**
* bcopy - Copy one area of memory to another
* @src: Where to copy from
* @dest: Where to copy to
* @count: The size of the area.
*
* Note that this is the same as memcpy(), with the arguments reversed.
* memcpy() is the standard, bcopy() is a legacy BSD function.
*
* You should not use this function to access IO space, use memcpy_toio()
* or memcpy_fromio() instead.
*/
char * bcopy(const char * src, char * dest, int count)
{
char *tmp = dest;

while (count--)
*tmp++ = *src++;

return dest;
}

13. memcpy
函数原型: void *memcpy(void *dest, const void *src, size_t n)
函数功能: 字符串拷贝
函数返回: 指向dest的指针
参数说明: src-源字符串,n-拷贝的最大长度
所属文件: <string.h>,<mem.h>
/**
* memcpy - Copy one area of memory to another
* @dest: Where to copy to
* @src: Where to copy from
* @count: The size of the area.
*
* You should not use this function to access IO space, use memcpy_toio()
* or memcpy_fromio() instead.
*/
void * memcpy(void * dest,const void *src,size_t count)
{
char *tmp = (char *) dest, *s = (char *) src;

while (count--)
*tmp++ = *s++;

return dest;
}
14. memcmp
/**
* memcmp - Compare two areas of memory
* @cs: One area of memory
* @ct: Another area of memory
* @count: The size of the area.
*/
int memcmp(const void * cs,const void * ct,size_t count)
{
const unsigned char *su1, *su2;
int res = 0;

for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
if ((res = *su1 - *su2) != 0)
break;
return res;
}
15. memscan
/**
* memscan - Find a character in an area of memory.
* @addr: The memory area
* @c: The byte to search for
* @size: The size of the area.
*
* returns the address of the first occurrence of @c, or 1 byte past
* the area if @c is not found
*/
void * memscan(void * addr, int c, size_t size)
{
unsigned char * p = (unsigned char *) addr;

while (size) {
if (*p == c)
return (void *) p;
p++;
size--;
}
return (void *) p;
}
16. strstr
/**
* strstr - Find the first substring in a %NUL terminated string
* @s1: The string to be searched
* @s2: The string to search for
*/
char * strstr(const char * s1,const char * s2)
{
int l1, l2;

l2 = strlen(s2);
if (!l2)
return (char *) s1;
l1 = strlen(s1);
while (l1 >= l2) {
l1--;
if (!memcmp(s1,s2,l2))
return (char *) s1;
s1++;
}
return NULL;
}

17. memchr
/**
* memchr - Find a character in an area of memory.
* @s: The memory area
* @c: The byte to search for
* @n: The size of the area.
*
* returns the address of the first occurrence of @c, or %NULL
* if @c is not found
*/
void *memchr(const void *s, int c, size_t n)
{
const unsigned char *p = s;
while (n-- != 0) {
if ((unsigned char)c == *p++) {
return (void *)(p-1);
}
}
return NULL;
}

string函数分析的更多相关文章

  1. 常用string函数分析

    string函数分析string函数包含在string.c文件中,经常被C文件使用.1. strcpy函数原型: char* strcpy(char* str1,char* str2);函数功能: 把 ...

  2. split&lpar;&rpar;&comma;preg&lowbar;split&lpar;&rpar;与explode&lpar;&rpar;函数分析与介

    split(),preg_split()与explode()函数分析与介 发布时间:2013-06-01 18:32:45   来源:尔玉毕业设计   评论:0 点击:965 split()函数可以实 ...

  3. Linux-0&period;11内核源代码分析系列:内存管理get&lowbar;free&lowbar;page&lpar;&rpar;函数分析

    Linux-0.11内存管理模块是源码中比較难以理解的部分,如今把笔者个人的理解发表 先发Linux-0.11内核内存管理get_free_page()函数分析 有时间再写其它函数或者文件的:) /* ...

  4. Javascript语言精粹之String常用方法分析

    Javascript语言精粹之String常用方法分析 1. String常用方法分析 1.1 String.prototype.slice() slice(start,end)方法复制string的 ...

  5. linux C函数之strdup函数分析【转】

    本文转载自:http://blog.csdn.net/tigerjibo/article/details/12784823 linux C函数之strdup函数分析 一.函数分析 1.函数原型: #i ...

  6. Python 常用string函数

    Python 常用string函数 字符串中字符大小写的变换 1. str.lower()   //小写>>> 'SkatE'.lower()'skate' 2. str.upper ...

  7. PHP String 函数

    [http://www.w3school.com.cn/php/php_ref_string.asp ] PHP String 简介 String 字符串函数允许您对字符串进行操作. 安装 Strin ...

  8. lua string函数

    lua的string函数: 参数中的index从1开始,负数的意义是从后开始往前数,比如-1代表最后一个字母 对于string类型的值,可以使用OO的方式处理,如string.byte(s.i)可以被 ...

  9. start&lowbar;amboot&lpar;&rpar;函数分析

    一.整体流程 start_amboot()函数是执行完start.S汇编文件后第一个C语言函数,完成的功能自然还是初始化的工作 . 1.全局变量指针r8设定,以及全局变量区清零 2.执行一些类初始化函 ...

随机推荐

  1. &lbrack;转&rsqb;ASP&period;NET Core 开发-Logging 使用NLog 写日志文件

    本文转自:http://www.cnblogs.com/Leo_wl/p/5561812.html ASP.NET Core 开发-Logging 使用NLog 写日志文件. NLog 可以适用于 . ...

  2. php基础的一点注意事项

    1.要弄懂"~"运算符的计算方法,首先必须明白二进制数在内存中的存放形式,二进制数在内存中是以补码的形式存放的 另外正数和负数的补码不一样,正数的补码,反码都是其本身,即: 正数9 ...

  3. Lucene学习笔记:一,全文检索的基本原理

    一.总论 根据http://lucene.apache.org/java/docs/index.html定义: Lucene是一个高效的,基于Java的全文检索库. 所以在了解Lucene之前要费一番 ...

  4. Android(java)学习笔记97:Scanner类使用

    package cn.itcast_01; /* * Scanner:用于接收键盘录入数据. * * 前面的时候: * A:导包 * B:创建对象 * C:调用方法 * * System类下有一个静态 ...

  5. 关于 hashCode&lpar;&rpar; 你需要了解的 3 件事

    (点击上方公众号,可快速关注) 原文:eclipsesource 译文:ImportNew - 南半球 链接:http://www.importnew.com/16517.html 在 Java 中, ...

  6. 微信公众平台PHP开发

    p=932" style="color: rgb(255, 153, 0); text-decoration: none;">微信公众平台PHP开发 2013.05 ...

  7. python3使用smtplib发电子邮件

    smtplib模块smtp简单邮件传输协议client实现.对于多功能性,有时,当你要发送带附件的邮件或图片,使用email.mime加载内容. 码,如以下: import smtplib impor ...

  8. python系统编程(五)

    多线程-threading python的thread模块是比较底层的模块,python的threading模块是对thread做了一些包装的,可以更加方便的被使用 1. 使用threading模块 ...

  9. 我所知道的JS调试

    前言 任何一门语言都有对应的调试方法,也有对应的调试工具,JavaScript当然也不例外.最常用的莫过于浏览器这个调试工具了.而今天我们要讲的对于这个基础调试就不细说,我会将目前所有调试javasc ...

  10. java框架之MyBatis&lpar;2&rpar;-进阶&amp&semi;整合Spring&amp&semi;逆向工程

    进阶内容 准备 jdbc.url=jdbc:mysql://192.168.208.192:3306/test?characterEncoding=utf-8 jdbc.driver=com.mysq ...