第一篇:C語言字符串操作總結(jié)大全(超詳細(xì))1[最終版]
C語言字符串操作總結(jié)大全(超詳細(xì))1)字符串操作
strcpy(p, p1)復(fù)制字符串
strncpy(p, p1, n)復(fù)制指定長度字符串
strcat(p, p1)附加字符串
strncat(p, p1, n)附加指定長度字符串
strlen(p)取字符串長度
strcmp(p, p1)比較字符串
strcasecmp忽略大小寫比較字符串 strncmp(p, p1, n)比較指定長度字符串
strchr(p, c)在字符串中查找指定字符
strrchr(p, c)在字符串中反向查找
strstr(p, p1)查找字符串
strpbrk(p, p1)以目標(biāo)字符串的所有字符作為集合,在當(dāng)前字符串查找該集合的任一元素
strspn(p, p1)以目標(biāo)字符串的所有字符作為集合,在當(dāng)前字符串查找不屬于該集合的任一元素的偏移
strcspn(p, p1)以目標(biāo)字符串的所有字符作為集合,在當(dāng)前字符串查找屬于該集合的任一元素的偏移
* 具有指定長度的字符串處理函數(shù)在已處理的字符串之后填補(bǔ)零結(jié)尾符
2)字符串到數(shù)值類型的轉(zhuǎn)換
strtod(p, ppend)從字符串 p 中轉(zhuǎn)換 double 類型數(shù)值,并將后續(xù)的字符串指針存儲(chǔ)到 ppend 指向的 char* 類型存儲(chǔ)。strtol(p, ppend, base)從字符串 p 中轉(zhuǎn)換 long 類型整型數(shù)值,base 顯式設(shè)置轉(zhuǎn)換的整型進(jìn)制,設(shè)置為 0 以根據(jù)特定格式判斷所用進(jìn)制,0x, 0X 前綴以解釋為十六進(jìn)制格式整型,0
前綴以解釋為八進(jìn)制格式整型 atoi(p)字符串轉(zhuǎn)換到 int 整型
atof(p)字符串轉(zhuǎn)換到 double 符點(diǎn)數(shù)
atol(p)字符串轉(zhuǎn)換到 long 整型
3)字符檢查
isalpha()檢查是否為字母字符
isupper()檢查是否為大寫字母字符
islower()檢查是否為小寫字母字符
isdigit()檢查是否為數(shù)字
isxdigit()檢查是否為十六進(jìn)制數(shù)字表示的有效字符
isspace()檢查是否為空格類型字符
iscntrl()檢查是否為控制字符
ispunct()檢查是否為標(biāo)點(diǎn)符號(hào)
isalnum()檢查是否為字母和數(shù)字
isprint()檢查是否是可打印字符
isgraph()檢查是否是圖形字符,等效于 isalnum()| ispunct()
4)函數(shù)原型
原型:strcpy(char destination[], const char source[]);功能:將字符串source拷貝到字符串destination中
例程:
#include
char str1[10] = { “TsinghuaOK”};
char str2[10] = { “Computer”};
cout < 運(yùn)行結(jié)果是:Computer 第二個(gè)字符串將覆蓋掉第一個(gè)字符串的所有內(nèi)容! 注意:在定義數(shù)組時(shí),字符數(shù)組1的字符串長度必須大于或等于字符串2的字符串長度。不能用賦值語句將一個(gè)字符串常量或字符數(shù)組直接賦給一個(gè)字符數(shù)組。所有字符串處理函數(shù)都包含在頭文件string.h中。 strncpy(char destination[], const char source[], int numchars); strncpy:將字符串source中前numchars個(gè)字符拷貝到字符串destination中。 strncpy函數(shù)應(yīng)用舉例 原型:strncpy(char destination[], const char source[], int numchars);功能:將字符串source中前numchars個(gè)字符拷貝到字符串destination中 例程: #include char str1[10] = { “Tsinghua ”}; char str2[10] = { “Computer”}; cout < 運(yùn)行結(jié)果:Comnghua 注意:字符串source中前numchars個(gè)字符將覆蓋掉字符串destination中前numchars個(gè)字符! 原型:strcat(char target[], const char source[]);功能:將字符串source接到字符串target的后面 例程: #include char str1[] = { “Tsinghua ”}; char str2[] = { “Computer”}; cout < 運(yùn)行結(jié)果:Tsinghua Computer 注意:在定義字符數(shù)組1的長度時(shí)應(yīng)該考慮字符數(shù)組2的長度,因?yàn)檫B接后新字符串的長度為兩個(gè)字符串長度之和。進(jìn)行字符串連接后,字符串1的結(jié)尾符將自動(dòng)被去掉,在結(jié)尾串末尾保留新字符串后面一個(gè)結(jié)尾符。 原型:strncat(char target[], const char source[], int numchars);功能:將字符串source的前numchars個(gè)字符接到字符串target的后面 例程: #include char str1[] = { “Tsinghua ”}; char str2[] = { “Computer”}; cout < 運(yùn)行結(jié)果:Tsinghua Com 原型:int strcmp(const char firststring[], const char secondstring);功能:比較兩個(gè)字符串firststring和secondstring 例程: #include char buf1[] = “aaa”; char buf2[] = “bbb”; char buf3[] = “ccc”; int ptr; ptr = strcmp(buf2,buf1); if(ptr > 0) cout <<“Buffer 2 is greater than buffer 1”< else cout <<“Buffer 2 is less than buffer 1”< ptr = strcmp(buf2,buf3); if(ptr > 0) cout <<“Buffer 2 is greater than buffer 3”< else cout <<“Buffer 2 is less than buffer 3”< 運(yùn)行結(jié)果是:Buffer 2 is less than buffer 1 Buffer 2 is greater than buffer 3 原型:strlen(const char string[]);功能:統(tǒng)計(jì)字符串string中字符的個(gè)數(shù) 例程: #include char str[100]; cout <<“請(qǐng)輸入一個(gè)字符串:”; cin >>str; cout <<“The length of the string is :”< 運(yùn)行結(jié)果The length of the string is x(x為你輸入的字符總數(shù)字) 注意:strlen函數(shù)的功能是計(jì)算字符串的實(shí)際長度,不包括'