@函數原型: char *strdup(const char *src)
函數功能: 字符串拷貝,目的空間由該函數分配
函數返回: 指向拷貝後的字符串指針
參數說明: src-待拷貝的源字符串
所屬文件: <string.h>
@函數名稱: strcpy
函數原型: char* strcpy(char* str1,char* str2);
函數功能: 把str2指向的字符串拷貝到str1中去
函數返回: 返回str1,即指向str1的指針
參數說明:
所屬文件: <string.h>
@函數名稱: strncpy
函數原型: char *strncpy(char *dest, const char *src,int count)
函數功能: 將字符串src中的count個字符拷貝到字符串dest中去
函數返回: 指向dest的指針
參數說明: dest-目的字符串,src-源字符串,count-拷貝的字符個數
所屬文件: <string.h>
- #include<stdio.h>
- #include<string.h>
- int main()
- {
- char*src = "bbbbbbbbbbbbbbbbbbbb";//20 'b's
- char dest[50] ="aaaaaaaaaaaaaaaaaaaa";//20 'a's
- puts(dest);
- strncpy(dest, src, 10);
- puts(dest);
- return0;
- }
- /*******************************************
- aaaaaaaaaaaaaaaaaaaa
- bbbbbbbbbbaaaaaaaaaa
- *******************************************/
@函數名稱: strcat
函數原型: char* strcat(char * str1,char * str2);
函數功能: 把字符串str2接到str1後面,str1最後的'\0'被取消
函數返回: str1
參數說明:
所屬文件: <string.h>
@函數名稱: strncat
函數原型: char *strncat(char *dest, const char *src, size_t maxlen)
函數功能: 將字符串src中前maxlen個字符連接到dest中
函數返回:
參數說明:
所屬文件: <string.h>
注意:與strncpy不同的是,strncat會自動在末尾加‘\0’,若指定長度超過源字符串長度,則只複製源字符串長度即停止
@函數名稱: strcmp
函數原型: int strcmp(char * str1,char * str2);
函數功能: 比較兩個字符串str1,str2.
函數返回: str1<str2,返回負數;str1=str2,返回 0;str1>str2,返回正數.
參數說明:
所屬文件: <string.h>
@函數名稱: strncmp
函數原型: int strncmp(char *str1,char *str2,int count)
函數功能: 對str1和str2中的前count個字符按字典順序比較
函數返回: 小於0:str1<str2,等於0:str1=str2,大於0:str1>str2
參數說明: str1,str2-待比較的字符串,count-比較的長度
所屬文件: <string.h>
@函數名稱: strpbrk
函數原型: char *strpbrk(const char *s1, const char *s2)
函數功能: 得到s1中第一個“同時也出現在s2中”字符的位置指針
函數返回: 位置指針
參數說明:
所屬文件: <string.h>
- #include<stdio.h>
- #include<string.h>
- int main()
- {
- char *p="Find all vowels";
- p=strpbrk(p+1,"aeiouAEIOU");
- while(p)
- {
- printf("%s\n",p);
- p=strpbrk(p+1,"aeiouAEIOU");
- }
- return 0;
- }
輸出:
- /**************************************
- ind all vowels
- all vowels
- owels
- els
- **************************************/
@函數名稱: strcspn
函數原型: int strcspn(const char *s1, const char *s2)
函數功能: 統計s1中從頭開始直到第一個“來自s2中的字符”出現的長度
函數返回: 長度
參數說明:
所屬文件: <string.h>
- #include<stdio.h>
- #include<string.h>
- int main()
- {
- printf("%d\n",strcspn("abcbcadef","cba"));
- printf("%d\n",strcspn("xxxbcadef","cba"));
- printf("%d\n",strcspn("123456789","cba"));
- return 0;
- }
輸出:
- /************************
- 0
- 3
- 9
- ************************/
@函數名稱: strspn
函數原型: int strspn(const char *s1, const char *s2)
函數功能: 統計s1中從頭開始直到第一個“不來自s2中的字符”出現的長度
函數返回: 位置指針
參數說明:
所屬文件: <string.h>
- #include<stdio.h>
- #include<string.h>
- #include<alloc.h>
- int main()
- {
- printf("%d\n",strspn("abcbcadef","cba"));
- printf("%d\n",strspn("xxxbcadef","cba"));
- printf("%d\n",strspn("123456789","cba"));
- return 0;
- }
- /************************
- 6
- 0
- 0
- ************************/
@函數名稱: strchr
函數原型: char* strchr(char* str,char ch);
函數功能: 找出str指向的字符串中第一次出現字符ch的位置
函數返回: 返回指向該位置的指針,如找不到,則返回空指針
參數說明: str-待搜索的字符串,ch-查找的字符
所屬文件: <string.h>
@函數名稱: strrchr
函數原型: char *strrchr(const char *s, int c)
函數功能: 得到字符串s中最後一個含有c字符的位置指針
函數返回: 位置指針
參數說明:
所屬文件: <string.h>
- #include<string.h>
- #include<stdio.h>
- int main()
- {
- charstring[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",c,ptr-string);
- else
- printf("The character was not found");
- return 0;
- }
@函數名稱: strstr
函數原型: char* strstr(char* str1,char* str2);
函數功能: 找出str2字符串在str1字符串中第一次出現的位置(不包括str2的串結束符)
函數返回: 返回該位置的指針,如找不到,返回空指針
@函數名稱: strrev
函數原型: char *strrev(char *s)
函數功能: 將字符串中的所有字符顛倒次序排列
函數返回: 指向s的指針
@函數名稱: strnset
函數原型: char *strnset(char *s, int ch, size_t n)
函數功能: 將字符串s中前n個字符設置爲ch的值
函數返回: 指向s的指針
@函數名稱: strupr
函數原型: char *strupr(char *s)
函數功能: 將字符串s中的字符變爲大寫
函數返回:
@函數名稱: strlwr
函數原型: char *strlwr(char *s)
函數功能: 將字符串中的字符變爲小寫字符
函數返回: 指向s的指針
@函數名稱: memcpy
函數原型: void *memcpy(void *dest, const void *src, size_t n)
函數功能: 字符串拷貝
函數返回: 指向dest的指針
參數說明: src-源字符串,n-拷貝的最大長度
所屬文件: <string.h>,<mem.h>
- #include <stdio.h>
- #include <string.h>
- int main()
- {
- char src[]="******************************";
- char dest[]="abcdefghijlkmnopqrstuvwxyz0123456709";
- char *ptr;
- printf("destination before memcpy:%s\n",dest);
- ptr=memcpy(dest,src,strlen(src));
- if (ptr)
- printf("destination after memcpy:%s\n",dest);
- else
- printf("memcpy failed");
- return 0;
- }
- /*************************************************************
- destination before memcpy:abcdefghijlkmnopqrstuvwxyz0123456709
- destination after memcpy:******************************456709
- **************************************************************/
@函數名稱: memccpy
函數原型: void *memccpy(void *dest, const void *src, int c, size_t n)
函數功能: 字符串拷貝,到指定長度或遇到指定字符時停止拷貝
函數返回:
參數說明: src-源字符串指針,c-中止拷貝檢查字符,n-長度,dest-拷貝底目的字符串指針
所屬文件: <string.h>,<mem.h>
- #include <string.h>
- #include <stdio.h>
- int main()
- {
- char *src="This is the source string";
- char dest[50];
- char *ptr;
- ptr=memccpy(dest,src,'c',strlen(src));
- if (ptr)
- {
- *ptr='\0';
- printf("The character wasfound:%s",dest);
- }
- else
- printf("The character wasn'tfound");
- return 0;
- }
- /*****************************************
- The character was found:This is the sourc
- *****************************************/
PS:指定字符被複制到dest中,memccpy返回了dest中指定字符的下一處的地址,返回NULL表示未遇到指定字符
@函數名稱: memchr
函數原型: void *memchr(const void *s, int c, size_t n)
函數功能: 在字符串中第開始n個字符中尋找某個字符c的位置
函數返回: 返回c的位置指針,返回NULL時表示未找到
參數說明: s-要搜索的字符串,c-要尋找的字符,n-指定長度
所屬文件: <string.h>,<mem.h>
- #include <string.h>
- #include <stdio.h>
- int main()
- {
- char str[17];
- char *ptr;
- strcpy(str,"This is a string");
- ptr=memchr(str,'r',strlen(str));
- if (ptr)
- printf("The character 'r' is at position:%d",ptr-str);
- else
- printf("The character was not found");
- return 0;
- }
@函數名稱: memcmp
函數原型: int memcmp(const void *s1, const void *s2,size_t n)
函數功能: 按字典順序比較兩個串s1和s2的前n個字節
函數返回: <0,=0,>0分別表示s1<,=,>s2
參數說明: s1,s2-要比較的字符串,n-比較的長度
@函數名稱: memicmp
函數原型: int memicmp(const void *s1, const void *s2, size_t n)
函數功能: 按字典順序、不考慮字母大小寫對字符串s1,s2前n個字符比較
函數返回: <0,=0,>0分別表示s1<,=,>s2
參數說明: s1,s2-要比較的字符串,n-比較的長度
所屬文件: <string.h>,<mem.h>
- #include <stdio.h>
- #include <string.h>
- int main()
- {
- char *buf1="ABCDE123";
- char *buf2="abcde456";
- int stat;
- stat=memicmp(buf1,buf2,5);
- printf("The strings to position 5 are");
- if(stat) printf("not");
- printf("the same");
- return 0;
- }
- /**************************************
- The strings to position 5 are the same
- ***************************************/
@函數名稱: memmove
函數原型: void *memmove(void *dest, const void *src, size_t n)
函數功能: 字符串拷貝
函數返回: 指向dest的指針
參數說明: src-源字符串,n-拷貝的最大長度
所屬文件: <string.h>,<mem.h>
- #include <string.h>
- #include <stdio.h>
- int main()
- {
- chardest[40]="abcdefghijklmnopqrstuvwxyz0123456789";
- printf("destination prior tomemmove:%s\n",dest);
- memmove(dest+1,dest,35);
- printf("destination aftermemmove:%s",dest);
- return 0;
- }
PS:與memcpy不同的是,memmove可以處理目的字符串與源字符串地址空間出現重疊的情況,可保證待複製的內容不被破壞。
@函數名稱: memset
函數原型: void *memset(void *s, int c, size_t n)
函數功能: 字符串中的n個字節內容設置爲c
函數返回:
參數說明: s-要設置的字符串,c-設置的內容,n-長度
所屬文件: <string.h>,<mem.h>
- #include <string.h>
- #include <stdio.h>
- #include <mem.h>
- int main()
- {
- charbuffer[]="Hello world";
- printf("Buffer before memset:%s/n",buffer);
- memset(buffer,'*',strlen(buffer)-1);
- printf("Buffer after memset:%s",buffer);
- return 0;
- }