linux c string库函数总结

来源:互联网 发布:数据透视图怎么做 编辑:程序博客网 时间:2024/05/05 22:54
#include<strings.h>

忽略大小比较两个字符是否相当。如果s1>s2返回一个大于0的数。
如果s1 = s2 返回一个0。如果s1<s2返回一个小于0的数。

/**********************************************************************************************************************************/

#strcmp(const char *s1, const char *s2)

#int strcasecmp(const char *s1, const char *s2);
#int strncasecmp(const char *s1, const char *s2, size_t n);
#
#DESCRIPTION
#The  strcasecmp() function compares the two strings s1 and s2, ignoring the case of the characters.  
#It returns an integer less than, equal to, or  greater than zero if s1 is found,
#respectively, to be less than, to match, or be greater than s2.
#The strncasecmp() function is similar,  except  it  only  compares  the  first n characters of s1.


DEMO

/*************************************************************************    > File Name: strcmp.c    > Author: 沉默羔羊    > Mail: zshh0604@163.com     > Created Time: 2014年11月08日 星期六 21时38分59秒 ************************************************************************/#include<stdio.h>#include<stdlib.h>#include<string.h>int main(int argc, char **argv){int i = 0;if(argc == 3 ){i = strcasecmp(argv[1],argv[2]);if(i>0){printf("argv[1] =%s  i = %d\n",argv[1],i);}else if(i<0){printf("argv[2] = %s\n,i = %d\n",argv[2],i);}else{ printf("i = %d\n",i);}}else{perror("args error \n"); exit(1);}exit(0);}


**********************************************************************************************************************************/


//获取字符c在s中出现的第一个位置,或者是最后一个位置。
/**********************************************************************************************************************************/
#include <strings.h>
#char *index(const char *s, int c);
#
#DESCRIPTION
#The  index()  function returns a pointer to the first occurrence of the character c in the string s.
#
#这个函数返回一个指针,它指向字符c在s中第一次出现的位置。
#
#
#char *rindex(const char *s, int c);
#The rindex() function returns a pointer to the last occurrence  of  the  character c in the string s.
#
#这个函数返回一个指针,它指向字符c在s中最后一次出现的位置。
#
#
#这两个参数的返回值:
#
#The index() and rindex() functions return  a  pointer  to  the  matched character or NULL if the character is not found.
#这个index()和rindex()函数。返回一个指向匹配的字符指针,或者是NULL是因为该字符没有被找到。

DEMO

/*************************************************************************    > File Name: index.c    > Author: 沉默羔羊    > Mail: zshh0604@163.com     > Created Time: 2014年11月08日 星期六 21时55分18秒 ************************************************************************/#include<stdio.h>#include<stdlib.h>#include<string.h>int main(int argc, char **argv){int i = 0;char * c;if(argc == 3 ){c = index(argv[1],(*argv[2]));fprintf(stdout,"%s\n",c);fflush(NULL);}else{perror("args error \n"); exit(1);}exit(0);}


/**********************************************************************************************************************************/




//字符串拷贝函数。
/**********************************************************************************************************************************/
#include <string.h>
#
#char *stpcpy(char *dest, const char *src);
#Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
#stpcpy():
# Since glibc 2.10:
#_XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
#Before glibc 2.1
#_GNU_SOURCE
#DESCRIPTION
#The  stpcpy()  function  copies the string pointed to by src (including the terminating null byte ('\0')) to the array pointed to by dest.
#The strings  may not overlap, and the destination string dest must be large enough to receive the copy. 。
#
#
/*************************************************************************    > File Name: stpcpy.c    > Author: 沉默羔羊    > Mail: zshh0604@163.com     > Created Time: 2014年11月08日 星期六 22时20分54秒 ************************************************************************/#include<stdio.h>#include<string.h>int main(void){char a[200] = "abcde";char *b = "dddd\n";stpcpy(a,b);printf("a = %s\n", a);}


//字符数组连接函数。src连接到dest后面

/**********************************************************************************************************************************/
#
#
##include <string.h>
#char *strcat(char *dest, const char *src);
#char *strncat(char *dest, const char *src, size_t n);
#DESCRIPTION
#The  strcat()  function  appends the src string to the dest string, overwriting the terminating null byte ('\0') at the end of dest,
#and then adds a terminating null byte.  The strings may not overlap, and the dest string must have enough space for the result. '')
#
#  将一个字符数组添加到目标字符数组中。他会给目标字符数组添加一个null作为目标的结束。这个字符也许会重合, 添加目标数组必须有足够的空间。
#  
#  这个函数和strncat功能类似,但是strncat有输入缓冲边界检查

和index功能一样。得到某个字符在字符数组中的第一个出现的位置。后者是最后一个出现的位置。
/**********************************************************************************************************************************/
# #include <string.h>
#
# char *strchr(const char *s, int c);
#
# char *strrchr(const char *s, int c);
#
# #define _GNU_SOURCE         /* See feature_test_macros(7) */
# #include <string.h>
# char *strchrnul(const char *s, int c);
#
#DESCRIPTION
#The strchr() function returns a pointer to the first occurrence of the character c in the string s.
#The strrchr() function returns a pointer to the last occurrence of the character c in the string s.
#
#//该函数返回一个指针,该指针指向字符c在s中的第一个位置。
#
#//该函数返回一个指针,该指针指向字符c在s中的最后一个位置。
#  
#
/*************************************************************************    > File Name: index.c    > Author: 沉默羔羊    > Mail: zshh0604@163.com     > Created Time: 2014年11月08日 星期六 21时55分18秒 ************************************************************************/#include<stdio.h>#include<stdlib.h>#include<string.h>int main(int argc, char **argv){int i = 0;char * c;if(argc == 3 ){//c = index(argv[1],(*argv[2]));c = strchr(argv[1],(*argv[2]));fprintf(stdout,"%s\n",c);fflush(NULL);}else{perror("args error \n"); exit(1);}exit(0);}



判断s中有多少个字符没有在accept中出现。
/**********************************************************************************************************************************/
#//返回连续多少个字符不没有在accept中出现。返回这个位置。
#  #include <string.h>
#  size_t strspn(const char *s, const char *accept);
#  size_t strcspn(const char *s, const char *reject);
#
#  DESCRIPTION
#  The strspn() function calculates the length of the initial segment of s which consists entirely of characters in accept.
#
#  The strcspn() function calculates the length of the initial segment  of s which consists entirely of characters not in reject.
#
#  RETURN VALUE
#   The  strspn()  function returns the number of characters in the initial segment of s which consist only of characters from accept.
#
#   The strcspn() function returns the number of characters in the  initial segment of s which are not in the string reject.
#
#
#
//使用一个分隔符号,将字符string分割成多个字符。
//调用函数后,返回值指向的是分隔符前面的指针。
//stringngp指向分隔符后面的字符。
/**********************************************************************************************************************************/
#char *strsep(char **stringp, const char *delim);
#include <string.h>
#char *strsep(char **stringp, const char *delim);
#
# Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
#     strsep(): _BSD_SOURCE
#
#DESCRIPTION
#If  *stringp is NULL, the strsep() function returns NULL and does nothing else.  Otherwise, this function finds the first token in the string *stringp,
#   
#   where tokens are delimited by symbols in the string delim.  
#
#This token is terminated by overwriting the delimiter with a null byte ('\0') and *stringp is updated to point past the token.  
#
#In case no delimiter was found, the token  is taken to be the entire string *stringp, and *stringp is made NULL.
#
#RETURN VALUE
#                                             
#The strsep() function returns a pointer to the token, that is, it returns the original value of *stringp.
#
#

/*************************************************************************    > File Name: index.c    > Author: 沉默羔羊    > Mail: zshh0604@163.com     > Created Time: 2014年11月08日 星期六 21时55分18秒 ************************************************************************/#include<stdio.h>#include<stdlib.h>#include<string.h>int main(int argc, char **argv){int i = 0;char * c;if(argc == 3 ){//c = index(argv[1],(*argv[2]));//c = strchr(argv[1],(*argv[2]));./a.out name=zshh0604 =//name = zshh0604c = strsep(&argv[1],argv[2]);fprintf(stdout,"%s\n",c);fprintf(stdout,"%s\n",argv[1]);fflush(NULL);}else{perror("args error \n"); exit(1);}exit(0);}

在一个函数中查找子串。
/**********************************************************************************************************************************/
#include <string.h>
#
#       char *strstr(const char *haystack, const char *needle);
#
#       #define _GNU_SOURCE         /* See feature_test_macros(7) */
#
#       #include <string.h>
#
#        char *strcasestr(const char *haystack, const char *needle);
#
#        DESCRIPTION
#       The strstr() function finds the first occurrence of the substring needle in the string haystack.  The terminating null bytes ('\0') are not compared.'')


/*************************************************************************    > File Name: strstr.c    > Author: 沉默羔羊    > Mail: zshh0604@163.com     > Created Time: 2014年11月08日 星期六 23时18分25秒 ************************************************************************/#include<stdio.h>#include<stdlib.h>#include<string.h>int main(void){char * a = "aaaabbbbb";char * b = "ab";char *c = NULL;c = strstr(a,b);printf("c = %s",c);exit(0);}


 
//将s复制为到一个malloc的空间,之后返回出来。
char *strdup(const char *s);

//将字符数组中的字符随机交换。
char *strfry(char *string);


查找字符数组s中如何一个字符第一次出现在accpet中的位置。
char *strpbrk(const char *s, const char *accept);


使用delim查找字符数组。
char *strtok(char *s, const char *delim);

//相当与拷贝函数。
size_t strxfrm(char *dest, const char *src, size_t n);

1 0
原创粉丝点击