c语言字符串函数详解
c语言字符串匹配函数

c语言字符串匹配函数1 介绍C语言字符串匹配函数C语言字符串匹配函数是一种用于在字符串中查找特定字符的函数,它可以在一个字符串的任意位置查找指定字符,或在字符串中搜索匹配指定模式的字符串。
C语言字符串匹配函数可帮助程序员进行字符串比较,以做出合理的判断。
C语言提供了多个用于字符串匹配的函数,其中最常用的是strstr(),用于搜索一个字符串中是否包含另一个字符串。
2 strstr()函数strstr()函数语法如下:char *strstr(const char *str1, const char *str2)该函数作用:在字符串str1中查找字符串str2,并返回匹配第一个字符的地址,如果没有找到则返回NULL,也就是NULL指针地址。
实例:char str[] = "This is a simple string";char * pch;pch = strstr(str, "simple");在str字符串中检索“simple”,返回“simple”在字符串中的地址。
3 其他字符串匹配函数除了strstr()函数外,还有下列一些字符串匹配函数可供程序员使用:strcmp()函数:比较两个字符串。
strcat()函数:把一个字符串连接到另一个字符串中。
strcmpi()函数:把两个字符串进行比较,忽略其大小写。
strncmp()函数:比较两个字符串的前面n个字符。
4 总结C语言的字符串匹配函数是一种非常便捷的搜索字符串的方法,能够帮助程序员快速搜索并比较字符串。
strstr()函数可以在一个字符串的任意位置查找指定字符,或在字符串中搜索匹配指定模式的字符串。
除此之外,还有多种字符串匹配函数,都可以根据开发需求进行选择。
C语言--字符串详解

C语⾔--字符串详解 字符串是⼀种⾮常重要的数据类型,但是C语⾔不存在显式的字符串类型,C语⾔中的字符串都以字符串常量的形式出现或存储在字符数组中。
同时,C 语⾔提供了⼀系列库函数来对操作字符串,这些库函数都包含在头⽂件 string.h 中。
⼀、字符串常量和字符数组1.1、什么是字符串常量 C 语⾔虽然没有字符串类型,但是 C语⾔提是存在字符串这个概念的,也就是字符串常量:以 NUL 字节结尾的 0 个或多个字符组成的序列。
字符串常量是不可被修改的,⼀般⽤⼀对双引号(" ")括起的⼀串字符来表⽰字符串常量,如: "Hello!"、"\aWarning!\a"、"123abc\n"、"" 字符串常量可以为空,如""就是⼀个空的字符串常量,但是即使为空,还是存在⼀个终⽌符 NUL 的。
(在 C 语⾔中,常⽤转义字符 \0来表⽰ NUL)1.2、字符串常量与指针 字符串常量与指针关系密切,因为字符串常量的值,实际上表⽰的是存储这些字符的内存空间的地址,更准确地说是字符串常量中第 1个字符的地址,⽽不是这些字符本⾝。
因此,在 C 语⾔中是不能直接进⾏字符串赋值的(因为没有字符串类型嘛)。
在 C 语⾔中,常通过声明⼀个指向 char 类型的指针并将其初始化为⼀个字符串常量的⽅式来访问⼀个字符串:char *message = "Hello World!";// 上⾯的语句也可以拆分成下⾯两句char *message;message = "Hello World!"; // 这句话看起来像是字符串复制,其实不是,只是涉及到指针操作 上述语句声明了⼀个指向 char 类型的指针,并⽤字符串常量中第 1 个字符的地址对该指针进⾏初始化。
可以通过字符指针 message 来访问字符串常量:#include <stdio.h>int main(){ char *message = "Hello World!"; printf("%s\n",message); while(*message != '\0'){ printf("%c ",*message++); } printf("\n"); return0;}/* output:* Hello World!* H e l l o W o r l d !*/ 这段代码,使⽤字符指针遍历了字符串常量中的每⼀个字符。
c语言字符串连接函数

c语言字符串连接函数C语言字符串连接函数包括strcat和strncat。
1. strcat函数:将src字符串连接到dest字符串的末尾,即把src的内容复制到dest的最后面,并在dest的结尾添加NULL字符。
声明:char *strcat(char *dest, const char *src);参数:- dest:目标字符串,即要连接的字符串- src:源字符串,即要被连接到目标字符串后面的字符串返回值:返回指向目标字符串dest的指针。
例子:#include <stdio.h>#include <string.h>int main(){char str1[20] = "Hello";char str2[20] = "World";strcat(str1, str2);printf("str1 = %s", str1);return 0;}输出结果:str1 = HelloWorld2. strncat函数:与strcat函数类似,但是可以指定拼接src字符串中前n个字符。
声明:char *strncat(char *dest, const char *src, size_t n);参数:- dest:目标字符串,即要连接的字符串- src:源字符串,即要被连接到目标字符串后面的字符串- n:拼接的字符数返回值:返回指向目标字符串dest的指针。
例子:#include <stdio.h>#include <string.h>int main(){char str1[20] = "Hello";char str2[20] = "World";strncat(str1, str2, 3);printf("str1 = %s", str1);return 0;}输出结果:str1 = HelloWor。
c字符串查找函数

c字符串查找函数在C语言中,有多种方法可以查找字符串。
其中最常用的是字符串查找函数。
字符串查找函数可以帮助我们快速地查找字符串中的某个特定字符或子串。
常用的字符串查找函数有以下几种:1. strstr()函数:该函数用于查找一个字符串中是否包含另一个字符串。
它的函数原型为:char *strstr(const char *str1, const char *str2);其中,str1是要查找的字符串,str2是要查找的子串。
如果找到了子串,则返回子串在字符串中第一次出现的位置;如果没有找到,则返回NULL。
2. strchr()函数:该函数用于在一个字符串中查找某个字符。
它的函数原型为:char *strchr(const char *str, int c);其中,str是要查找的字符串,c是要查找的字符。
如果找到了字符,则返回该字符在字符串中第一次出现的位置;如果没有找到,则返回NULL。
3. strrchr()函数:该函数与strchr()函数类似,只不过它是从字符串的末尾开始查找。
它的函数原型为:char *strrchr(const char *str, int c);其中,str是要查找的字符串,c是要查找的字符。
如果找到了字符,则返回该字符在字符串中最后一次出现的位置;如果没有找到,则返回NULL。
4. strpbrk()函数:该函数用于在一个字符串中查找包含某些字符的任意一个字符。
它的函数原型为:char *strpbrk(const char *str1, const char *str2);其中,str1是要查找的字符串,str2是包含要查找的字符的字符串。
如果找到了包含的字符,则返回该字符在字符串中第一次出现的位置;如果没有找到,则返回NULL。
5. strstr()函数:该函数用于在一个字符串中查找一个子串的最后一次出现位置。
它的函数原型为:char *strrstr(const char *str1, const char *str2);其中,str1是要查找的字符串,str2是要查找的子串。
C语言字符串函数大全

今天总结了下C语言字符串函数。
C语言字符串函数总结:1.字符处理库(ctype)中的函数2.stdio中的字符串和字符输入/输出的函数3.通用实用库stdlib中的字符串转换函数4.字符串处理库string中的字符串处理函数C语言的字符串实际上是存储单个字符的数组,结尾包含一个结束该字符串的特别的字符("空字符",用'\0'表示)。
char string1[]="first"实际上有6个元素。
char color="blue" char * p="blue"注意p[i]不能修改,若需修改应用字符数组。
一、.字符处理库(ctype)中的函数#include<ctype.h>函数原型:int f(int c)isdigit, isalpha, isalnum, isxdigit, islower, isupper, tolower, toupper, isspace,空白字符:新行符\n, 空格,回车''\r",水平制表符"\t", 垂直制表符"\v" isctrl, ispunct, isprint, isalpha二、stdio中的字符串和字符输入/输出的函数int getchar(void) 从标准输入设备读取字符以整数返回char * get(char * s) 从标准输入设备读入字符到数组s直到遇到新行符和文件结束符为止,然后再数组后追加NULL字符int putchar(int c) 打印字符int puts(const char * s) 打印字符串s和新行符int sprintf(char * s, const char * format) 与printf区别在于输出结果存放在s中int sscanf(char * s, const char * format); 与scanf区别在于从数组s读取数据示例1 字符串反转#include <stdio.h> void reverse(char *s) {if(s[0] == '\0') return;else{reverse(&s[1]); putchar(s[0]);}}int main(){char s[100];gets(s);reverse(s);return 0;}输入:sf输出:fs示例2 sscanf和sprintf #include<stdio.h>int main(){int x=1;double y=2.1;char s[100];sprintf(s,"Hello!%d, %f", x, y);puts(s);sscanf(s,"%d%f",&x,&y);printf("x:%d, y:%f", x, y);return 0;}输出:Hello!1, 2.100000x:1, y:2.100000三、stdlib中的字符串转换函数#include<stdlib.h>1. atoi(将字符串转换成整型数)定义函数int atoi(const char *nptr); 函数说明atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回。
c语言string函数

c语言string函数
标准 C 语言的string.h头文件中定义了一组有用的函数,被称为字符串处理函数。
字符串就是由字母,数字和符号组成的一行字符序列,通常以字符串结尾显示。
它是一种数据抽象,是用来存储,分析,表示和处理应用程序中的文本数据的一种常见方式。
1.strlen()函数:该函数用来计算字符串的长度,不包括字符串结尾的null字符
'\0'。
该函数所返回的字符数不包括null字符,而只代表字符串中实际的字符数。
2.strcat()函数:该函数的作用是将字符串s2拼接到字符串s1的末尾。
这意味着s1的末尾会出现字符串s2的第一个字符,直到结束符'\0'。
4.strcmp()函数:该函数用于比较两个字符串s1和s2,如果s1>s2,函数会返回正值;如果s1=s2,函数会返回0;如果s1
5.strchr()函数:该函数用于在字符串s1中查找给定字符c。
如果成功,函数会返回指向找到字符的指针;否则,函数会返回NULL。
7.strtok()函数:该函数用于将字符串分割成一系列子字符串,每个子字符串以一个或多个指定的分隔符分隔。
以上就是标准C语言的string.h头文件中的一些常用的字符串处理函数,这些函数主要用于对字符串的操作,如切割,拼接,比较等。
由于 C 语言的数据结构比较简单,因此这个头文件函数的功能也是非常强大的,可以满足各种字符串处理的需求。
C语言~所有的字符串操作函数

7.函数名: strcspn 功 能: 在串中查找第一个给定字符集内容的段 用 法: int strcspn(char *str1, char *str2); 程序例: #i nclude <stdio.h> #i nclude <string.h> #i nclude <alloc.h> int main(void) { char *string1 = "1234567890"; char *string2 = "747DC8"; int length;
printf("buffer 2 is greater than buffer 1\n"); if (ptr < 0)
printf("buffer 2 is less than buffer 1\n"); if (ptr == 0)
printf("buffer 2 equals buffer 1\n"); return 0; }
printf("buffer 2 is less than buffer 1\n"); if (ptr == 0)
printf("buffer 2 equals buffer 1\n"); return 0; }
14.函数名: strncpy 功 能: 串拷贝 用 法: char *strncpy(char *destin, char *source, int maxlen); 程序例: #i nclude <stdio.h> #i nclude <string.h> int main(void) {
c语言常用函数大全及详解

C语言常用函数包括:1.printf函数:用于在控制台输出数据。
2.scanf函数:用于从控制台读取用户输入的数据。
3.strlen函数:用于计算字符串的长度。
4.strcmp函数:用于比较两个字符串的大小。
5.strcpy函数:用于将一个字符串复制到另一个字符串中。
6.strcat函数:用于将一个字符串连接到另一个字符串的末尾。
7.strchr函数:用于查找字符串中指定字符的位置。
8.strstr函数:用于查找字符串中指定子串的位置。
9.atoi函数:用于将字符串转换为整数。
10.atof函数:用于将字符串转换为浮点数。
11.malloc函数:用于动态分配内存空间。
12.free函数:用于释放动态分配的内存空间。
13.memcpy函数:用于将一段内存区域的数据复制到另一段内存区域。
14.memset函数:用于将一段内存区域的数据设置为指定的值。
15.abs函数:用于计算整数的绝对值。
16.rand函数:用于生成随机数。
17.srand函数:用于设置随机数生成器的种子。
18.time函数:用于获取当前的系统时间。
19.localtime函数:用于将时间戳转换为本地时间。
20.strtol函数:用于将字符串转换为长整型数。
21.strtod函数:用于将字符串转换为双精度浮点数。
22.fprintf函数:用于将数据格式化输出到文件中。
23.fscanf函数:用于从文件中读取格式化的数据。
24.fgets函数:用于从文件中读取一行数据。
25.fputs函数:用于将数据写入文件中。
26.fopen函数:用于打开文件。
27.fclose函数:用于关闭文件。
28.feof函数:用于判断文件是否已经到达文件末尾。
29.ferror函数:用于判断文件操作是否发生错误。
30.fprintf函数:用于将数据格式化输出到文件中。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
void *_memccpy(void *dest, const void *src, int c, size_t count);从src复制0个或多个字节的字符到dest. 当字符c被复制或者count个字符被复制时, 复制停止. 如果字符c被复制, 函数返回这个字符后面紧挨一个字符位置的指针. 否则返回NULL.int memcmp(const void *buf1, const void *buf2, size_t count);比较buf1和buf2前面count个字节大小.返回值< 0, 表示buf1小于buf2;返回值为0, 表示buf1等于buf2;返回值> 0, 表示buf1大于buf2.int memicmp(const void *buf1, const void *buf2, size_t count);比较buf1和buf2前面count个字节. 与memcmp不同的是, 它不区分大小写.返回值同上.size_t strlen(const char *string);获取字符串长度, 字符串结束符NULL不计算在内.没有返回值指示操作错误.char *strrev(char *string);将字符串string中的字符顺序颠倒过来. NULL结束符位置不变.返回调整后的字符串的指针.char *_strupr(char *string);将string中所有小写字母替换成相应的大写字母, 其它字符保持不变.返回调整后的字符串的指针.char *_strlwr(char *string);将string中所有大写字母替换成相应的小写字母, 其它字符保持不变.返回调整后的字符串的指针.char *strchr(const char *string, int c);查找字符c在字符串string中首次出现的位置, NULL结束符也包含在查找中.返回一个指针, 指向字符c在字符串string中首次出现的位置, 如果没有找到, 则返回NULL.char *strrchr(const char *string, int c);查找字符c在字符串string中最后一次出现的位置, 也就是对string进行反序搜索, 包含NULL结束符. 返回一个指针, 指向字符c在字符串string中最后一次出现的位置, 如果没有找到, 则返回NULL.char *strstr(const char *string, const char *strSearch);在字符串string中查找strSearch子串.返回子串strSearch在string中首次出现位置的指针. 如果没有找到子串strSearch, 则返回NULL. 如果子串strSearch为空串, 函数返回string值.char *strdup(const char *strSource);函数运行中会自己调用malloc函数为复制strSource字符串分配存储空间, 然后再将strSource复制到分配到的空间中. 注意要及时释放这个分配的空间.返回一个指针, 指向为复制字符串分配的空间; 如果分配空间失败, 则返回NULL值.char *strcat(char *strDestination, const char *strSource);将源串strSource添加到目标串strDestination后面, 并在得到的新串后面加上NULL结束符. 源串strSource的字符会覆盖目标串strDestination后面的结束符NULL. 在字符串的复制或添加过程中没有溢出检查, 所以要保证目标串空间足够大. 不能处理源串与目标串重叠的情况.函数返回strDestination值.char *strncat(char *strDestination, const char *strSource, size_t count);将源串strSource开始的count个字符添加到目标串strDest后. 源串strSource的字符会覆盖目标串strDestination后面的结束符NULL. 如果count大于源串长度, 则会用源串的长度值替换count值. 得到的新串后面会自动加上NULL结束符. 与strcat函数一样, 本函数不能处理源串与目标串重叠的情况.函数返回strDestination值.char *strcpy(char *strDestination, const char *strSource);复制源串strSource到目标串strDestination所指定的位置, 包含NULL结束符. 不能处理源串与目标串重叠的情况.函数返回strDestination值.char *strncpy(char *strDestination, const char *strSource, size_t count);将源串strSource开始的count个字符复制到目标串strDestination所指定的位置. 如果count值小于或等于strSource串的长度, 不会自动添加NULL结束符目标串中, 而count大于strSource串的长度时, 则将strSource用NULL结束符填充补齐count个字符, 复制到目标串中. 不能处理源串与目标串重叠的情况. 函数返回strDestination值.char *strset(char *string, int c);将string串的所有字符设置为字符c, 遇到NULL结束符停止.函数返回内容调整后的string指针.char *strnset(char *string, int c, size_t count);将string串开始count个字符设置为字符c, 如果count值大于string串的长度, 将用string的长度替换count值.函数返回内容调整后的string指针.size_t strspn(const char *string, const char *strCharSet);查找任何一个不包含在strCharSet串中的字符(字符串结束符NULL除外) 在string串中首次出现的位置序号.返回一个整数值, 指定在string中全部由characters中的字符组成的子串的长度. 如果string以一个不包含在strCharSet中的字符开头, 函数将返回0值.size_t strcspn(const char *string, const char *strCharSet);查找strCharSet串中任何一个字符在string串中首次出现的位置序号, 包含字符串结束符NULL.返回一个整数值, 指定在string中全部由非characters中的字符组成的子串的长度. 如果string以一个包含在strCharSet中的字符开头, 函数将返回0值.char *strspnp(const char *string, const char *strCharSet);查找任何一个不包含在strCharSet串中的字符(字符串结束符NULL除外) 在string串中首次出现的位置指针.返回一个指针, 指向非strCharSet中的字符在string中首次出现的位置.char *strpbrk(const char *string, const char *strCharSet);查找strCharSet串中任何一个字符在string串中首次出现的位置, 不包含字符串结束符NULL.返回一个指针, 指向strCharSet中任一字符在string中首次出现的位置. 如果两个字符串参数不含相同字符, 则返回NULL值.int strcmp(const char *string1, const char *string2);比较字符串string1和string2大小.返回值< 0, 表示string1小于string2;返回值为0, 表示string1等于string2;返回值> 0, 表示string1大于string2.int stricmp(const char *string1, const char *string2);比较字符串string1和string2大小,和strcmp不同, 比较的是它们的小写字母版本.返回值与strcmp相同.int strcmpi(const char *string1, const char *string2);等价于stricmp函数, 只是提供一个向后兼容的版本.int strncmp(const char *string1, const char *string2, size_t count);比较字符串string1和string2大小,只比较前面count个字符. 比较过程中, 任何一个字符串的长度小于count, 则count将被较短的字符串的长度取代. 此时如果两串前面的字符都相等, 则较短的串要小.返回值< 0, 表示string1的子串小于string2的子串;返回值为0, 表示string1的子串等于string2的子串;返回值> 0, 表示string1的子串大于string2的子串.int strnicmp(const char *string1, const char *string2, size_t count);比较字符串string1和string2大小,只比较前面count个字符. 与strncmp不同的是, 比较的是它们的小写字母版本.返回值与strncmp相同.char *strtok(char *strToken, const char *strDelimit);在strToken 串中查找下一个标记, strDelimit字符集则指定了在当前查找调用中可能遇到的分界符.返回一个指针, 指向在strToken中找到的下一个标记. 如果找不到标记, 就返回NULL值. 每次调用都会修改strToken内容, 用NULL字符替换遇到的每个分界符.函数名: stpcpy功能: 拷贝一个字符串到另一个用法: char *stpcpy(char *destin, char *source); 程序例:#include <stdio.h>#include <string.h>int main(void){char string[10];char *str1 = "abcdefghi";stpcpy(string, str1);printf("%s\n", string);return 0;}函数名: strcat功能: 字符串拼接函数用法: char *strcat(char *destin, char *source); 程序例:#include <string.h>#include <stdio.h>int main(void){char destination[25];char *blank = " ", *c = "C++", *Borland = "Borland"; strcpy(destination, Borland);strcat(destination, blank);strcat(destination, c);printf("%s\n", destination);return 0;}函数名: strchr功能: 在一个串中查找给定字符的第一个匹配之处\用法: char *strchr(char *str, char c);程序例:#include <string.h>#include <stdio.h>{char string[15];char *ptr, c = 'r';strcpy(string, "This is a string");ptr = strchr(string, c);if (ptr)printf("The character %c is at position: %d\n", c, ptr-string);elseprintf("The character was not found\n");return 0;}函数名: strcmp功能: 串比较用法: int strcmp(char *str1, char *str2);看Asic码,str1>str2,返回值> 0;两串相等,返回0程序例:#include <string.h>#include <stdio.h>{char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";int ptr;ptr = strcmp(buf2, buf1);if (ptr > 0)printf("buffer 2 is greater than buffer 1\n");elseprintf("buffer 2 is less than buffer 1\n");ptr = strcmp(buf2, buf3);if (ptr > 0)printf("buffer 2 is greater than buffer 3\n");elseprintf("buffer 2 is less than buffer 3\n");return 0;}函数名: strncmpi功能: 将一个串中的一部分与另一个串比较, 不管大小写用法: int strncmpi(char *str1, char *str2, unsigned maxlen); 程序例:#include <string.h>#include <stdio.h>int main(void){char *buf1 = "BBB", *buf2 = "bbb";int ptr;ptr = strcmpi(buf2, buf1);if (ptr > 0)printf("buffer 2 is greater than buffer 1\n"); if (ptr < 0)printf("buffer 2 is less than buffer 1\n");if (ptr == 0)printf("buffer 2 equals buffer 1\n");return 0;}函数名: strcpy功能: 串拷贝用法: char *strcpy(char *str1, char *str2); 程序例:#include <stdio.h>#include <string.h>int main(void){char string[10];char *str1 = "abcdefghi";strcpy(string, str1);printf("%s\n", string);return 0;}函数名: strcspn功能: 在串中查找第一个给定字符集内容的段用法: int strcspn(char *str1, char *str2); 程序例:#include <stdio.h>#include <string.h>#include <alloc.h>int main(void){char *string1 = "1234567890";char *string2 = "747DC8";int length;length = strcspn(string1, string2);printf("Character where strings intersect is at position %d\n", length);return 0;}函数名: strdup功能: 将串拷贝到新建的位置处用法: char *strdup(char *str);程序例:#include <stdio.h>#include <string.h>#include <alloc.h>int main(void){char *dup_str, *string = "abcde";dup_str = strdup(string);printf("%s\n", dup_str);free(dup_str);return 0;}函数名: stricmp功能: 以大小写不敏感方式比较两个串用法: int stricmp(char *str1, char *str2);程序例:#include <string.h>#include <stdio.h>int main(void){char *buf1 = "BBB", *buf2 = "bbb";int ptr;ptr = stricmp(buf2, buf1);if (ptr > 0)printf("buffer 2 is greater than buffer 1\n");if (ptr < 0)printf("buffer 2 is less than buffer 1\n"); if (ptr == 0)printf("buffer 2 equals buffer 1\n"); return 0;}函数名: strerror功能: 返回指向错误信息字符串的指针用法: char *strerror(int errnum);程序例:#include <stdio.h>#include <errno.h>int main(void){char *buffer;buffer = strerror(errno);printf("Error: %s\n", buffer);return 0;}函数名: strcmpi功能: 将一个串与另一个比较, 不管大小写用法: int strcmpi(char *str1, char *str2);程序例:#include <string.h>#include <stdio.h>int main(void){char *buf1 = "BBB", *buf2 = "bbb";int ptr;ptr = strcmpi(buf2, buf1);if (ptr > 0)printf("buffer 2 is greater than buffer 1\n"); if (ptr < 0)printf("buffer 2 is less than buffer 1\n");if (ptr == 0)printf("buffer 2 equals buffer 1\n");return 0;}函数名: strncmp功能: 串比较用法: int strncmp(char *str1, char *str2, int maxlen);程序例:#include <string.h>#include <stdio.h>int main(void){char *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc"; int ptr;ptr = strncmp(buf2,buf1,3);if (ptr > 0)printf("buffer 2 is greater than buffer 1\n");elseprintf("buffer 2 is less than buffer 1\n");ptr = strncmp(buf2,buf3,3);if (ptr > 0)printf("buffer 2 is greater than buffer 3\n");elseprintf("buffer 2 is less than buffer 3\n");return(0);}函数名: strncmpi功能: 把串中的一部分与另一串中的一部分比较, 不管大小写用法: int strncmpi(char *str1, char *str2);程序例:#include <string.h>#include <stdio.h>int main(void){char *buf1 = "BBBccc", *buf2 = "bbbccc";int ptr;ptr = strncmpi(buf2,buf1,3);if (ptr > 0)printf("buffer 2 is greater than buffer 1\n");if (ptr < 0)printf("buffer 2 is less than buffer 1\n");if (ptr == 0)printf("buffer 2 equals buffer 1\n");return 0;}函数名: strncpy功能: 串拷贝用法: char *strncpy(char *destin, char *source, int maxlen); 程序例:#include <stdio.h>#include <string.h>int main(void){char string[10];char *str1 = "abcdefghi";strncpy(string, str1, 3);string[3] = '\0';printf("%s\n", string);return 0;}函数名: strnicmp功能: 不注重大小写地比较两个串用法: int strnicmp(char *str1, char *str2, unsigned maxlen); 程序例:#include <string.h>#include <stdio.h>int main(void){char *buf1 = "BBBccc", *buf2 = "bbbccc";int ptr;ptr = strnicmp(buf2, buf1, 3);if (ptr > 0)printf("buffer 2 is greater than buffer 1\n");if (ptr < 0)printf("buffer 2 is less than buffer 1\n");if (ptr == 0)printf("buffer 2 equals buffer 1\n");return 0;}函数名: strnset功能: 将一个串中的所有字符都设为指定字符用法: char *strnset(char *str, char ch, unsigned n); 程序例:#include <stdio.h>#include <string.h>int main(void){char *string = "abcdefghijklmnopqrstuvwxyz"; char letter = 'x';printf("string before strnset: %s\n", string); strnset(string, letter, 13);printf("string after strnset: %s\n", string);return 0;}函数名: strpbrk功能: 在串中查找给定字符集中的字符用法: char *strpbrk(char *str1, char *str2);程序例:#include <stdio.h>#include <string.h>int main(void){char *string1 = "abcdefghijklmnopqrstuvwxyz"; char *string2 = "onm";char *ptr;ptr = strpbrk(string1, string2);if (ptr)printf("strpbrk found first character: %c\n", *ptr); elseprintf("strpbrk didn't find character in set\n"); return 0;}函数名: strrchr功能: 在串中查找指定字符的最后一个出现用法: char *strrchr(char *str, char c);程序例:#include <string.h>#include <stdio.h>int main(void){char string[15];char *ptr, c = 'r';strcpy(string, "This is a string");ptr = strrchr(string, c);if (ptr)printf("The character %c is at position: %d\n", c, ptr-string); elseprintf("The character was not found\n");return 0;}函数名: strrev功能: 串倒转用法: char *strrev(char *str);程序例:#include <string.h>#include <stdio.h>int main(void){char *forward = "string";printf("Before strrev(): %s\n", forward); strrev(forward);printf("After strrev(): %s\n", forward); return 0;}函数名: strset功能: 将一个串中的所有字符都设为指定字符用法: char *strset(char *str, char c);程序例:#include <stdio.h>#include <string.h>int main(void){char string[10] = "123456789";char symbol = 'c';printf("Before strset(): %s\n", string);strset(string, symbol);printf("After strset(): %s\n", string);return 0;}函数名: strspn功能: 在串中查找指定字符集的子集的第一次出现用法: int strspn(char *str1, char *str2);程序例:#include <stdio.h>#include <string.h>#include <alloc.h>int main(void){char *string1 = "1234567890";char *string2 = "123DC8";int length;length = strspn(string1, string2);printf("Character where strings differ is at position %d\n", length); return 0;}函数名: strstr功能: 在串中查找指定字符串的第一次出现用法: char *strstr(char *str1, char *str2);程序例:#include <stdio.h>#include <string.h>int main(void){char *str1 = "Borland International", *str2 = "nation", *ptr; ptr = strstr(str1, str2);printf("The substring is: %s\n", ptr);return 0;}函数名: strtod功能: 将字符串转换为double型值用法: double strtod(char *str, char **endptr);程序例:#include <stdio.h>#include <stdlib.h>int main(void){char input[80], *endptr;double value;printf("Enter a floating point number:");gets(input);value = strtod(input, &endptr);printf("The string is %s the number is %lf\n", input, value); return 0;}函数名: strtok功能: 查找由在第二个串中指定的分界符分隔开的单词用法: char *strtok(char *str1, char *str2);程序例:#include <string.h>#include <stdio.h>int main(void){char input[16] = "abc,d";char *p;/* strtok places a NULL terminatorin front of the token, if found */p = strtok(input, ",");if (p) printf("%s\n", p);/* A second call to strtok using a NULLas the first parameter returns a pointerto the character following the token */p = strtok(NULL, ",");if (p) printf("%s\n", p);return 0;}函数名: strtol功能: 将串转换为长整数用法: long strtol(char *str, char **endptr, int base); 程序例:#include <stdlib.h>#include <stdio.h>int main(void){char *string = "87654321", *endptr;long lnumber;/* strtol converts string to long integer */ lnumber = strtol(string, &endptr, 10);printf("string = %s long = %ld\n", string, lnumber); return 0;}函数名: strupr功能: 将串中的小写字母转换为大写字母用法: char *strupr(char *str);程序例:#include <stdio.h>#include <string.h>int main(void){char *string = "abcdefghijklmnopqrstuvwxyz", *ptr;/* converts string to upper case characters */ptr = strupr(string);printf("%s\n", ptr);return 0;}函数名: swab功能: 交换字节用法: void swab (char *from, char *to, int nbytes); 程序例:#include <stdlib.h>#include <stdio.h>#include <string.h>char source[15] = "rFna koBlrna d";char target[15];int main(void){swab(source, target, strlen(source));printf("This is target: %s\n", target);return 0;}C语言中利用strtok函数进行字符串分割C语言不像Java,Php之类的高级语言,对象中直接封装了字符串的处理函数。