当前位置:文档之家› c语言字符串函数详解

c语言字符串函数详解

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之类的高级语言,对象中直接封装了字符串的处理函数。

相关主题