字符串函数<string.h>

在头文件<string.h>中定义了两组字符串函数。第一组函数的名字以str开头;第二组函数的名字以mem开头。只有函数memmove对重叠对象间的拷贝进行了定义,而其他函数都未定义。比较类函数将其变量视为unsigned char类型的数组。

1 strcpy

#include <string.h>

char *strcpy(char *str1, const char *str2);

把字符串str2(包括'\0')拷贝到字符串str1当中,并返回str1。

2 strncpy

#include <string.h>

char *strncpy(char *str1, const char *str2, size_t count);

把字符串str2中最多count个字符拷贝到字符串str1中,并返回str1。如果str2中少于count个字符,那么就用'\0'来填充,直到满足count个字符为止。

3 strcat

#include <string.h>

char *strcat(char *str1, const char *str2);

把str2(包括'\0')拷贝到str1的尾部(连接),并返回str1。其中终止原str1的'\0'被str2的第一个字符覆盖。

4 strncat

#include <string.h>

char *strncat(char *str1, const char *str2, size_t count);

把str2中最多count个字符连接到str1的尾部,并以'\0'终止str1,返回str1。其中终止原str1的'\0'被str2的第一个字符覆盖。

注意,最大拷贝字符数是count+1。

5 strcmp

#include <string.h>

int strcmp(const char *str1, const char *str2);

按字典顺序比较两个字符串,返回整数值的意义如下:

  • 小于0,str1小于str2;
  • 等于0,str1等于str2;
  • 大于0,str1大于str2;

6 strncmp

#include <string.h>

int strncmp(const char *str1, const char *str2, size_t count);

同strcmp,除了最多比较count个字符。根据比较结果返回的整数值如下:

  • 小于0,str1小于str2;
  • 等于0,str1等于str2;
  • 大于0,str1大于str2;

7 strchr

#include <string.h>

char *strchr(const char *str, int ch);

返回指向字符串str中字符ch第一次出现的位置的指针,如果str中不包含ch,则返回NULL。

8 strrchr

#include <string.h>

char *strrchr(const char *str, int ch);

返回指向字符串str中字符ch最后一次出现的位置的指针,如果str中不包含ch,则返回NULL。

9 strspn

#include <string.h>

size_t strspn(const char *str1, const char *str2);

返回字符串str1中由字符串str2中字符构成的第一个子串的长度。

10 strcspn

#include <string.h>

size_t strcspn(const char *str1, const char *str2);

返回字符串str1中由不在字符串str2中字符构成的第一个子串的长度。

11 strpbrk

#include <string.h>

char *strpbrk(const char *str1, const char *str2);

返回指向字符串str2中的任意字符第一次出现在字符串str1中的位置的指针;如果str1中没有与str2相同的字符,那么返回NULL。

12 strstr

#include <string.h>

char *strstr(const char *str1, const char *str2);

返回指向字符串str2第一次出现在字符串str1中的位置的指针;如果str1中不包含str2,则返回NULL。

13 strlen

#include <string.h>

size_t strlen(const char *str);

返回字符串str的长度,'\0'不算在内。

14 strerror

#include <string.h>

char *strerror(int errnum);

返回指向与错误序号errnum对应的错误信息字符串的指针(错误信息的具体内容依赖于实现)。

15 strtok

#include <string.h>

char *strtok(char *str1, const char *str2);

在str1中搜索由str2中的分界符界定的单词。

对strtok()的一系列调用将把字符串str1分成许多单词,这些单词以str2中的字符为分界符。第一次调用时str1非空,它搜索str1,找出由非str2中的字符组成的第一个单词,将str1中的下一个字符替换为'\0',并返回指向单词的指针。随后的每次strtok()调用(参数str1用NULL代替),均从前一次结束的位置之后开始,返回下一个由非str2中的字符组成的单词。当str1中没有这样的单词时返回NULL。每次调用时字符串str2可以不同。

如:

char *p;

p = strtok("The summer soldier,the sunshine patriot", " ");

printf("%s", p);

do {

p = strtok("\0", ", "); /* 此处str2是逗号和空格 */

if (p)

printf("|%s", p);

} while (p);

显示结果是:The | summer | soldier | the | sunshine | patriot

16 memcpy

#include <string.h>

void *memcpy(void *to, const void *from, size_t count);

把from中的count个字符拷贝到to中。并返回to。

17 memmove

#include <string.h>

void *memmove(void *to, const void *from, size_t count);

功能与memcpy类似,不同之处在于,当发生对象重叠时,函数仍能正确执行。

18 memcmp

#include <string.h>

int memcmp(const void *buf1, const void *buf2, size_t count);

比较buf1和buf2的前count个字符,返回值与strcmp的返回值相同。

19 memchr

#include <string.h>

void *memchr(const void *buffer, int ch, size_t count);

返回指向ch在buffer中第一次出现的位置指针,如果在buffer的前count个字符当中找不到匹配,则返回NULL。

20 memset

#include <string.h>

void *memset(void *buf, int ch, size_t count);

把buf中的前count个字符替换为ch,并返回buf。

c语言字符串库函数#include<string.h>的更多相关文章

  1. c语言实用功能库函数#include<stdlib.h>

    实用函数<stdlib.h> 在头文件<stdlib.h>中说明了用于数值转换.内存分配以及具有其他相似任务的函数. 1 atof #include <stdlib.h& ...

  2. c语言时间库函数#include<time.h>

    日期与时间函数<time.h> 头文件<time.h>中说明了一些用于处理日期和时间的类型和函数.其中的一部分函数用于处理当地时间,因为时区等原因,当地时间与日历时间可能不相同 ...

  3. #include <string.h>

    1 _memccpy 2 _memicmp 3 _strlwr 4 _strrev 5 _strset 6 _strupr 7 memccpy 8 memchr 9 memcpy 10 memicmp ...

  4. 字符串操作函数<string.h>相关函数strcpy,strcat,等源码。

    首先说一下源码到底在哪里找. 我们在文件中包含<cstring>时,如果点击右键打开文档, 会打开cstring,我们会发现路径为: D:\Program Files\visual stu ...

  5. 5. 常见C语言字符串库函数的使用及实现

    1. strncat 函数: [函数原型]#include <string.h> char *strncat( char *str1, const char *str2, size_t c ...

  6. #include<string.h>

    #include<string.h> 1 strcpy #include <string.h> char *strcpy(char *str1, const char *str ...

  7. #include<iostream>与#include<iostream.h>以及#inclue<string> 和 #include<string.h>的区别

    转载于:http://www.cnblogs.com/charley_yang/archive/2010/12/08/1900715.html 1.从功能性的角度来讲,<iostream> ...

  8. [C++]在什么时候需要“#include string.h“

    相关资料:https://zhidao.baidu.com/question/515578726.html C++中,string头文件基本上已经包含在iostream中了.但是,平时使用的时候建议加 ...

  9. c语言输入与输出库函数#include<stdio.h>

    last modified: 2010-05-28 输入与输出<stdio.h> 头文件<stdio.h>定义了用于输入和输出的函数.类型和宏.最重要的类型是用于声明文件指针的 ...

随机推荐

  1. 2017 google Round D APAC Test 题解

    首先说明一下:我只是用暴力过了4道题的小数据,就是简单的枚举,大数据都不会做!下面的题解,是我从网上搜到的解答以及查看排行榜上大神的答案得出来的. 首先贴一下主要的题解来源:http://codefo ...

  2. ajaxSubmit() 上传文件和进度条显示

    1.  首先引用js文件 <script type="text/javascript" src="/js/jquery/jquery.form.js"&g ...

  3. Linux查找软件的安装路径

    软件安装的路径可能不止一个,可以使用whereis命令查看软件安装的所有路径,以mysql为例: whereis mysql 该命令会返回软件的所有安装路径: mysql: /usr/bin/mysq ...

  4. Python3 面向对象

    Class 在Python中,定义类是通过class关键字: class Student(object): pass class后面紧接着是类名,即Student,类名通常是大写开头的单词,紧接着是( ...

  5. Keil 4 与Proteus 7.8联调

    实验环境: windows 8.1 pro with Keil 4 and Proteus 7.8 both cracked. 步骤: 下载联调工具Vdmagdi,安装. keil下Option/De ...

  6. 探究为何rem在chrome浏览器上计算出错

    最近在一个项目中,测试同学提了一个bug,说手机上有个页面的某些字体显示偏大.就像这样 我用chrome浏览器在pc上测试了一下,发现pc上也有这个问题,但是用其它浏览器打开这个页面就没有发现这个问题 ...

  7. Vijos p1165 火烧赤壁 离散化+单调栈

    题目链接:https://vijos.org/p/1165 题意:输入n(n <= 20,000)段线段的端点,问所有线段的长度总和为多少? input: -1 1 5 11 2 9 outpu ...

  8. 创建Mysql

    CREATE DATABASE IF NOT EXISTS yiya DEFAULT CHARSET utf8 COLLATE utf8_general_ci;

  9. uCOS-II任务的挂起和恢复

    函数描述 OSTaskSuspend() 功能描述:无条件挂起一个任务.调用此函数的任务也可以传递参数OS_PRIO_SELF,挂起调用任务本身.函数原型:INT8U OSTaskSuspend ( ...

  10. Unity3d Shader开发(四)UsePass ,GrabPass ,SubShader Tags

    (一)UsePass 命令 使用 来自另一个着色器的命名通道. Syntax 语法 UsePass "Shader/Name" 插入所有来自给定着色器中的给定名字的通道.Shade ...