c語言string.h頭文件中所有函數(shù)示例詳解
前言
頭文件<string.h>中有兩大類字符串函數(shù)。一類為str開頭的函數(shù),一類men開頭的函數(shù)。men開頭的函數(shù),效率會(huì)更高一些。
參數(shù)中 size_t 是一種無符號整數(shù)類型,確保不會(huì)出現(xiàn)負(fù)數(shù)。
一 . str開頭
1. strcpy() 復(fù)制
原型:char * strcpy(char *dest, char *src);
功能:src指向的字符 復(fù)制到dest中
返回:dest
#include<stdio.h> #include<string.h> int main(void) { char desn[10]; char* source="hello world!"; strcpy(desn,source); printf("源字符串 :%s\n",source); printf("目標(biāo)字符串:%s\n",desn); return 0; }
結(jié)果:
2. strncpy() 復(fù)制
原型:char * strncpy(char *dest, char *src, size_t maxsize)
功能:src指向的字符 復(fù)制到dest中,并返回dest,限制最大字符串長度 maxsize。當(dāng)src中少于maxsize時(shí),使用‘\0’填充。其中size_t是無符號整型,保證maxsize始終為正數(shù)。
返回: dest
示例:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char * src="hello,world!"; char dest[20]; strncpy(dest, src, 20); printf("源字符串 :%s\n",src); printf("目標(biāo)字符串:%s\n",dest); system("pause"); return(0); }
運(yùn)行結(jié)果:
3. strcat() 合并
原型:char *strcat(char *dest, const char *src)
功能:將字符串src 連接到dest的尾部。
返回: dest
示例:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char * src="world" ; char dest[50] ="hello,"; /*目標(biāo)字符串必須聲明足夠的長度,足夠連接src字符串長度*/ strcat(dest, src); printf("目標(biāo)字符串:%s\n",dest); system("pause"); return(0); }
運(yùn)行:
4. strncat() 合并
原型:char *strcat(char *dest, const char *src,size_t maxsize)
功能:將字符串src 最多前maxsize個(gè)字符 連接到dest的尾部。src中必須有字符串結(jié)束符'\0'
返回: dest
示例:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char * src="world" ; char dest[50] ="hello,"; /*目標(biāo)字符串必須聲明足夠的長度,足夠連接src字符串長度*/ strncat(dest, src,3); printf("目標(biāo)字符串:%s\n",dest); system("pause"); return(0); }
運(yùn)行:
5. strcmp() 比較
原型:int strcmp(const char *str1, const char *str2)
功能:比較字符串str1和str2
返回: 當(dāng)str1<str2,返回值小于0
當(dāng)str1=str2,返回值等于0
當(dāng)str1>str2,返回值大于0
示例:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char * str1 ="hello,world!" ; char * str2 ="hello,human!"; int res= strcmp(str1, str2); if(res<0) printf("str1小于str2"); else if(res==0) printf("str1等于str2"); else printf("str1大于str2"); printf("\n"); system("pause"); return(0); }
運(yùn)行結(jié)果:
6. strncmp() 比較
原型:int strncmp(const char *str1, const char *str2,size_t maxsize)
功能:字符串str1中至多maxsize個(gè)字符與字符串str2比較
返回: 當(dāng)str1<str2,返回值小于0
當(dāng)str1=str2,返回值等于0
當(dāng)str1>str2,返回值大于0
示例:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char * str1 ="hello,world!" ; char * str2 ="hello,human!"; int res= strncmp(str1, str2,5); if(res<0) printf("str1小于str2\n"); else if(res==0) printf("str1等于str2\n"); else printf("str1大于str2\n"); printf("\n"); system("pause"); return(0); }
運(yùn)行結(jié)果:
發(fā)現(xiàn)和strcmp()中的結(jié)果不一樣,是因?yàn)閟trncmp(str1, str2,5)中,只比較了前5個(gè)字符。前5個(gè)字符都是hello,所以相等。
7. strchr() 查找
原型:char *strchr(const char * src, int c)
功能:查找字符c在src中的第一次出現(xiàn)的位置
返回: 字符c在src中的第一次出現(xiàn)位置的指針
示例:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char * str ="hello,world!" ; char * pos; pos= strchr(str, 'o'); printf("\n 字符第一次出現(xiàn)的位置為第%d個(gè)(從0開始),內(nèi)存地址為:0x%x\n", pos-str,pos ); system("pause"); return(0); }
運(yùn)行結(jié)果:
8. strrchr() 查找
原型:char * strrchr(const char * src, int c)
功能:查找字符c在src中的最后一次出現(xiàn)的位置
返回: 字符c在src中的最后一次出現(xiàn)位置的指針
示例:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char * str ="hello,world!" ; char * pos; pos= strrchr(str, 'o'); printf("\n 字符最后一次出現(xiàn)的位置為第%d個(gè)(從0開始),內(nèi)存地址為:0x%x\n", pos-str,pos ); system("pause"); return(0); }
運(yùn)行結(jié)果:
9. strcspn() 查找
原型:size_t strcspn(const char * str1, const char * str2)
功能:返回字符串 str1 中包含str2中字符的前綴的長度?;蛘呖梢岳斫鉃?strong>字符串 str1 中第一個(gè)在字符串 str2 中出現(xiàn)的字符下標(biāo) 。直接看代碼和運(yùn)行結(jié)果,看完就明白了。
示例:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char * str1 ="world!" ; char * str2="jbgar"; int len; len = strcspn(str1, str2);/*world中r在ar中出現(xiàn)了,它的下標(biāo)是2*/ printf("\nstr1中找到第一個(gè)在str2中出現(xiàn)的字母,它的下標(biāo)為:%d\n", len); system("pause"); return(0); }
運(yùn)行結(jié)果:
10. strspn() 查找
原型:size_t strspn(const char * str1, const char * str2)
功能:返回字符串 str1 中不包含str2中字符的前綴的長度?;蛘呖梢岳斫鉃?nbsp;查找字符串 str1 中第一個(gè)沒有在字符串 str2 中出現(xiàn)的字符下標(biāo) 。直接看代碼和運(yùn)行結(jié)果,看完就明白了。這個(gè)容易和strcspn()函數(shù)搞反,得格外注意。
示例:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char * str1 ="world!" ; char * str2="dewor"; int len; len = strspn(str1, str2);/*world中l(wèi)在是第一個(gè)沒有在dewor中出現(xiàn),它的下標(biāo)是3*/ printf("\nstr1中找到第一個(gè)沒有在str2中出現(xiàn)的字母,它的下標(biāo)為:%d\n", len); system("pause"); return(0); }
運(yùn)行結(jié)果:
11. strpbrk() 查找
原型:char * strpbrk(const char * str1, const char * str2)
功能:查找str2的任意字符在str1中第一次出現(xiàn)的位置,并返回指向這個(gè)位置的指針
返回: 字符指針
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char * str1 ="world!" ; char * str2="glar"; char * pos; pos = strpbrk(str1, str2);/*glar中的r是第一個(gè)在world中出現(xiàn),將返回r的位置, 雖然glar中的l在world中也出現(xiàn),但是它不是第一個(gè)在world中出現(xiàn)的*/ printf("\nstr2的字符在str1中第一個(gè)出現(xiàn)的字母是%c,它在str1的下標(biāo)為%d\n", *pos,pos-str1); system("pause"); return(0); }
運(yùn)行結(jié)果:
12. strstr() 查找
原型:char * strstr(const char * str1, const char * str2)
功能:查找str2在在str1中第一次出現(xiàn)的位置,并返回指向這個(gè)位置的指針
返回: 字符指針
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char * str1 ="wohello,world!" ; char * str2="wor"; char * pos; pos = strstr(str1, str2); printf("\n\"wor\"在\"wohello,world\"中第一個(gè)出現(xiàn)的位置下標(biāo)為%d\n", pos-str1); system("pause"); return(0); }
運(yùn)行結(jié)果:
13. strlen() 計(jì)算長度
原型:size_t strstr(const char * str)
功能:返回str的長度
示例:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char * str1 ="hello,world!" ; int len = strlen(str1); printf("\n\str1的長度:%d\n", len); system("pause"); return(0); }
運(yùn)行結(jié)果:
14. strerror() 錯(cuò)誤碼
原型:char * strerror(int n)
功能:傳入不同的錯(cuò)誤碼時(shí) 返回對應(yīng)的錯(cuò)誤字符串。這個(gè)函數(shù)很有意思哦。
示例:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { printf("\n \040 0 :%s \n", strerror(0)); /* \040是空格的轉(zhuǎn)義字符 */ printf("\n \040 1 :%s \n", strerror(1)); printf("\n \040 2 :%s \n", strerror(2)); printf("\n \040 3 :%s \n", strerror(3)); printf("\n \040 4 :%s \n", strerror(4)); printf("\n \040 5 :%s \n", strerror(5)); printf("\n \040 6 :%s \n", strerror(6)); printf("\n \040 -1 :%s \n", strerror(-1)); system("pause"); return(0); }
運(yùn)行結(jié)果:
15. strtok() 截取
原型:char * strtok(char* str1,const char* str2)
功能:使用str2,對str1字符串進(jìn)行分隔截取,返回截取的部分字符串
示例:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char str [] ="hello@world@coffee@tea!" ; char * c="@" ; printf("\n第一個(gè):%s\n", strtok(str,c)); printf("\n第二個(gè):%s\n", strtok(NULL,c)); printf("\n第三個(gè):%s\n", strtok(NULL,c)); printf("\n第四個(gè):%s\n", strtok(NULL,c)); printf("\n再重新開始:%s\n", strtok(str,c)); system("pause"); return(0); }
運(yùn)行結(jié)果:
二. mem開頭
men開頭的函數(shù),主要目的是提供一個(gè)高效的函數(shù)接口。
1. memcpy() 復(fù)制
原型:void * memcpy( char * str1, const char * str2 ,int n)
功能:將字符串str2中的n個(gè)字符拷貝到str1中;
示例:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char str1[13] ; char * str2="hello,world!" ; memcpy(str1,str2,13); printf("\n\str1中字符為:%s\n", str1); system("pause"); return(0); }
運(yùn)行結(jié)果:
2. memcmp() 比較
原型:int memcmp(const char * str1, const char * str2 ,int n)
功能:str1中的前n個(gè)字符和str2中的值進(jìn)行比較。
返回:
當(dāng)str1<str2時(shí),返回值<0
當(dāng)str1=bstr2時(shí),返回值=0
當(dāng)str1>str2時(shí),返回值>0
示例:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char * str1="hello,world!" ; char * str2="helle" ; char * resstr; int res=memcmp(str1,str2,5); if(res<0) resstr="小于"; else if(res=0) resstr="等于"; else resstr="大于"; printf("\n\str1的前5個(gè)字符%sstr2的字符\n", resstr); system("pause"); return(0); }
運(yùn)行結(jié)果:
3. memchr() 查找
原型:char * memchr(const char * str, char c ,int n)
功能:在str中前n個(gè)字符中查找 c的位置,返回找到的位置的指針,如果找不到返回NULL
示例:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char * str="hello,world!" ; char c=',' ; char * pos; pos=(char*) memchr(str,c,10); printf("\n\",\"在\"hello,world\"的下標(biāo)位置為:%d\n", pos-str); system("pause"); return(0); }
運(yùn)行結(jié)果:
4. memset() 替換
原型:void * memset( void * ptr, int value ,size_t n)
功能:此函數(shù)初始化函數(shù),將某一塊內(nèi)存(參數(shù)ptr)中的內(nèi)容全部設(shè)置為指定的值 value
將ptr中的前n個(gè)數(shù)據(jù)替換為value,并返回ptr,
示例1:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char str []="hello,world!" ; char c='w' ; memset(str,c,5); printf("\n\str中字符串:%s\n", str); system("pause"); return(0); }
運(yùn)行結(jié)果:
示例2:
結(jié)構(gòu)變量初始化,將結(jié)構(gòu)中的變量用value初始化。
#include <stdio.h> #include <stdlib.h> typedef struct manager { int iArr [12]; float fArr [5]; char ch; //整型int賦值給char,只要數(shù)組在-128到127之間就沒問題 } Manager; //打印manager中的值 void printManager(Manager manager) { int i=0; puts("iArr值:"); for(; i<12; i++) //打印數(shù)組iArr的所有元素 printf("第%d個(gè): %d\n",i+1, manager.iArr[i]); puts("\nmanager->fArr值:"); for(i=0; i<5; i++) //打印數(shù)組fArr的所有元素 printf("第%d個(gè): %f\n",i+1, manager.fArr[i]); puts("\nmanager->ch值:"); printf("%c\n", manager.ch); } void memsetTest() { Manager manager ; printf("使用65初始化\n"); memset(&manager,65,sizeof(manager)); printManager(manager); printf("\n使用0初始化\n"); memset(&manager,0,sizeof(manager)); printManager(manager); } int main() { memsetTest(); getchar(); return 0; }
返回結(jié)果:
5. memmove() 替換
原型:void * memmove( char * str1, const char * str2 ,int n)
功能:將s中的前n個(gè)字符替換為c,并返回s,功能和memcpy一樣,但是可以解決linux下memcpy因?yàn)榈刂分丿B而造成替換失敗的問題。算是對memcpy的一個(gè)升級。在windows系統(tǒng)中沒有這個(gè)問題。
示例:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char * src="hello,world!"; char dest[20]; memmove(dest, src, 20); printf("源字符串 :%s\n",src); printf("目標(biāo)字符串:%s\n",dest); system("pause"); return(0); }
運(yùn)行結(jié)果:
總結(jié)
到此這篇關(guān)于c語言string.h頭文件中所有函數(shù)示例詳解的文章就介紹到這了,更多相關(guān)c語言string.h頭文件函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
函數(shù)指針與指針函數(shù)的學(xué)習(xí)總結(jié)
函數(shù)指針是指向函數(shù)的指針,指針函數(shù)是指一個(gè)函數(shù)的返回值是一個(gè)指針。以下就是對函數(shù)指針與指針函數(shù)的應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以參考下2013-07-07Qt利用ImageWatch實(shí)現(xiàn)圖片查看功能
Visual Studio有專門針對OpenCV開發(fā)的插件,名叫ImageWatch,圖片放大之后可以查看RGB的像素值。本文將利用這一查件實(shí)現(xiàn)圖片查看功能,需要的可以參考一下2022-04-04C++實(shí)現(xiàn)LeetCode(133.克隆無向圖)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(133.克隆無向圖),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07C++利用 _findfirst與_findnext查找文件的方法
這篇文章主要給大家介紹了關(guān)于C++利用 _findfirst與_findnext查找文件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06