linux c string庫函數總結

#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: [email protected] 
    > 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: [email protected] 
    > 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: [email protected] 
    > 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: [email protected] 
    > 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: [email protected] 
    > 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 = zshh0604
		c = 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: [email protected] 
    > 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);

發佈了58 篇原創文章 · 獲贊 65 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章