今天總結(jié)了下C語言字符串函數(shù)。
C語言字符串函數(shù)總結(jié):
1.字符處理庫(ctype)中的函數(shù)
2.stdio中的字符串和字符輸入/輸出的函數(shù)
3.通用實用庫stdlib中的字符串轉(zhuǎn)換函數(shù)
4.字符串處理庫string中的字符串處理函數(shù)
C語言的字符串實際上是存儲單個字符的數(shù)組,結(jié)尾包含一個結(jié)束該字符串的特別的字符("空字符",用'\0'表示)。 char string1[]="first"實際上有6個元素。
char color="blue" char * p="blue"
注意p[i]不能修改,若需修改應(yīng)用字符數(shù)組。
一、.字符處理庫(ctype)中的函數(shù)
#include<ctype.h>
函數(shù)原型:int f(int c)
isdigit, isalpha, isalnum, isxdigit, islower, isupper, tolower, toupper,
isspace,空白字符:新行符\n, 空格,回車''\r",水平制表符"\t", 垂直制表符"\v"
isctrl, ispunct, isprint, isalpha
二、stdio中的字符串和字符輸入/輸出的函數(shù)
int getchar(void) 從標(biāo)準(zhǔn)輸入設(shè)備讀取字符以整數(shù)返回
char * get(char * s) 從標(biāo)準(zhǔn)輸入設(shè)備讀入字符到數(shù)組s直到遇到新行符和文件結(jié)束符為止,然后再數(shù)組后追加NULL字符
int putchar(int c) 打印字符
int puts(const char * s) 打印字符串s和新行符
int sprintf(char * s, const char * format) 與printf區(qū)別在于輸出結(jié)果存放在s中
int sscanf(char * s, const char * format); 與scanf區(qū)別在于從數(shù)組s讀取數(shù)據(jù)
示例1 字符串反轉(zhuǎn)
#include <stdio.h>
void reverse(char *s)
{
if(s[0] == '\0')
return;
else
{
reverse(&s[1]);
putchar(s[0]);
}
}
int main()
{
char s[100];
gets(s);
reverse(s);
return 0;
}
輸入:sf
輸出:fs
示例2 sscanf和sprintf
#include<stdio.h>
int main()
{
int x=1;
double y=2.1;
char s[100];
sprintf(s,"Hello!%d, %f", x, y);
puts(s);
sscanf(s,"%d%f",&x,&y);
printf("x:%d, y:%f", x, y);
return 0;
}
輸出:
Hello!1, 2.100000
x:1, y:2.100000
三、stdlib中的字符串轉(zhuǎn)換函數(shù)
#include<stdlib.h>
1. atoi(將字符串轉(zhuǎn)換成整型數(shù)) 定義函數(shù) int atoi(const char *nptr); 函數(shù)說明 atoi()會掃描參數(shù)nptr字符串,跳過前面的空格字符,直到遇上數(shù)字或正負符號才開始做轉(zhuǎn)換,而再遇到非數(shù)字或字符串結(jié)束時('\0')才結(jié)束轉(zhuǎn)換,并將結(jié)果返回。 返回值 返回轉(zhuǎn)換后的整型數(shù)。
附加說明 atoi()與使用strtol(nptr,(char**)NULL,10);結(jié)果相同。
2. atof(將字符串轉(zhuǎn)換成浮點型數(shù)) 定義函數(shù) double atof(const char *nptr); 函數(shù)說明 atof()會掃描參數(shù)nptr字符串,跳過前面的空格字符,直到遇上數(shù)字或正負符號才開始做轉(zhuǎn)換,而再遇到非數(shù)字或字符串結(jié)束時('\0')才結(jié)束轉(zhuǎn)換,并將結(jié)果返回。參數(shù)nptr字符串可包含正負號、小數(shù)點或E(e)來表示指數(shù)部分,如123.456或123e-2。 返回值 返回轉(zhuǎn)換后的浮點型數(shù)。 附加說明 atof()與使用strtod(nptr,(char**)NULL)結(jié)果相同。
3. atol(將字符串轉(zhuǎn)換成長整型數(shù)) 定義函數(shù) long atol(const char *nptr); 函數(shù)說明 atol()會掃描參數(shù)nptr字符串,跳過前面的空格字符,直到遇上數(shù)字或正負符號才開始做轉(zhuǎn)換,而再遇到非數(shù)字或字符串結(jié)束時('\0')才結(jié)束轉(zhuǎn)換,并將結(jié)果返回。 返回值 返回轉(zhuǎn)換后的長整型數(shù)。 附加說明 atol()與使用strtol(nptr,(char**)NULL,10);結(jié)果相同。
4. strtod(將字符串轉(zhuǎn)換成浮點數(shù)) 定義函數(shù) double strtod(const char *nptr,char **endptr); 函數(shù)說明 strtod()會掃描參數(shù)nptr字符串,跳過前面的空格字符,直到遇上數(shù)字或正負符號才開始做轉(zhuǎn)換,到出現(xiàn)非數(shù)字或字符串結(jié)束時('\0')才結(jié)束轉(zhuǎn)換,并將結(jié)果返回。若endptr不為NULL,則會將遇到不合條件而終止的nptr中的字符指針由endptr傳回。參數(shù)nptr字符串可包含正負號、小數(shù)點或E(e)來表示指數(shù)部分。如123.456或123e-2。 返回值 返回轉(zhuǎn)換后的浮點型數(shù)。
5.strtol(將字符串轉(zhuǎn)換成長整型數(shù)) 定義函數(shù) long int strtol(const char *nptr,char **endptr,int base); 函數(shù)說明 strtol()會將參數(shù)nptr字符串根據(jù)參數(shù)base來轉(zhuǎn)換成長整型數(shù)。參數(shù)base范圍從2至36,或0。參數(shù)base代表采用的進制方式,如base值為10則采用10進制,若base值為16則采用16進制等。當(dāng)base值為0時則是采用10進制做轉(zhuǎn)換,但遇到如'0x'前置字符則會使用16進制做轉(zhuǎn)換。一開始strtol()會掃描參數(shù)nptr字符串,跳過前面的空格字符,直到遇上數(shù)字或正負符號才開始做轉(zhuǎn)換,再遇到非數(shù)字或字符串結(jié)束時('\0')結(jié)束轉(zhuǎn)換,并將結(jié)果返回。若參數(shù)endptr不為NULL,則會將遇到不合條件而終止的nptr中的字符指針由endptr返回。 返回值 返回轉(zhuǎn)換后的長整型數(shù),否則返回ERANGE并將錯誤代碼存入errno中。 附加說明 ERANGE指定的轉(zhuǎn)換字符串超出合法范圍。
6. strtoul(將字符串轉(zhuǎn)換成無符號長整型數(shù)) 定義函數(shù) unsigned long int strtoul(const char *nptr,char **endptr,int base); 函數(shù)說明 strtoul()會將參數(shù)nptr字符串根據(jù)參數(shù)base來轉(zhuǎn)換成無符號的長整型數(shù)。參數(shù)base范圍從2至36,或0。參數(shù)base代表采用的進制方式,如base值為10則采用10進制,若base值為16則采用16進制數(shù)等。當(dāng)base值為0時則是采用10進制做轉(zhuǎn)換,但遇到如'0x'前置字符則會使用16進制做轉(zhuǎn)換。一開始strtoul()會掃描參數(shù)nptr字符串,跳過前面的空格字符串,直到遇上數(shù)字或正負符號才開始做轉(zhuǎn)換,再遇到非數(shù)字或字符串結(jié)束時('\0')結(jié)束轉(zhuǎn)換,并將結(jié)果返回。若參數(shù)endptr不為NULL,則會將遇到不合條件而終止的nptr中的字符指針由endptr返回。 返回值 返回轉(zhuǎn)換后的長整型數(shù),否則返回ERANGE并將錯誤代碼存入errno中。 附加說明 ERANGE指定的轉(zhuǎn)換字符串超出合法范圍。
示例:
#include <stdio.h>
#include <stdlib.h>
int main()
{
double d;
char * string="51.2 String";
char * strPtr;
d=strtod(string, &strPtr);
printf("%f And %s", d, strPtr);
return 0;
}
輸出:51.200000 And String
四、string.h中的字符串處理函數(shù)
(一)字符操作函數(shù)
1. char * strcpy(char * s1, const char * s2) 把s2拷貝到s1,返回s1的值
2. char * strncpy(char * s1, const char * s2, size_t n) 拷貝s2中的n個字符到s1
3. char * strcat(char * s1, const char * s2) 把s2追加到s1后邊
4. char * strncat(char * s1, const char * s2, size_t n) 把s2的n個字符追加到s1后
注意:
strncpy不一定拷貝第二個參數(shù)的終止符'\0'(僅當(dāng)n至少比s2長度大1時才拷貝)
但strncat則會自動將'\0'拷貝到結(jié)果后邊。
#include<stdio.h> #include<string.h> int main() { char x[]="Happy EveryDay"; char y[]="Happy"; char z[100]; //strcpy printf("strcpy Test:"); printf("%s\n",strcpy(z,x)); //strncpy printf("strncpy Test:"); strncpy(z,x,5); z[5]='\0'; puts(z); //strcat printf("strcat Test:"); printf("%s\n", strcat(z,y)); //strncat printf("strncat Test:"); printf("%s\n", strncat(z,y,2)); return 0; } 輸出: strcpy Test:Happy EveryDay strncpy Test:Happy strcat Test:HappyHappy strncat Test:HappyHappyHa
(二)比較函數(shù)
1. int strcmp(const char * s1, const char * s2) 比較字符串s1和s2,小于、等于、大于分別返回負值、0、正值
2. int strncmp(const char * s1, const char * s2, size_t n) 比較字符串s1和s2的n個字符,結(jié)果同strcmp(不比較'\0'后的字符)
(三)查找函數(shù)
1. char * strchar(const char * s, int c) 返回指向字符串s中字符c首次出現(xiàn)的指針,沒有返回NULL
2. char * strrchar(const char * s, int c) 返回指向字符串s中字符c最后一次出現(xiàn)的指針,沒有返回NULL
3. char * strstr(const char * s1, const char * s2) 返回指向字符串s1中首次出現(xiàn)s2位置的指針,無返回NULL
4. size_t strspn(const char * s1, const char * s2) 返回s1中只包含s2中字符的起始段的長度
5. size_t strcspn(const char * s1, const char * s2) 返回s1中不包含s2中字符的起始段的長度
6. char * strpbrk(const char * s1, const char * s2) 返回指向s1中首次出現(xiàn)s2中字符的位置的指針,沒有返回NULL
7. char * strtok(char * s1, const char * s2) 將s1打斷為用s2中包含的字符分開的記號。第1次調(diào)用把s1作為參數(shù),以后為繼續(xù)把該字符串打斷成記號而再次調(diào)用strtok時要用NULL作為第一個參數(shù),每次調(diào)用都返回指向當(dāng)前記號的指針,字符串沒有剩余記號時返回NULL。 注意:strtok會修改輸入的字符串,故請拷貝后再調(diào)用。 示例: #include<stdio.h> #include<string.h> int main() { char str[]="This is a string"; char * tokenPtr; tokenPtr=strtok(str, " "); while(tokenPtr != NULL) { puts(tokenPtr); tokenPtr=strtok(NULL," "); } return 0; } 輸出: This is a string
(四)內(nèi)存函數(shù)
用來操作、比較和查詢內(nèi)存塊,操作對象為”內(nèi)存塊“。
1. void * memcpy(void * s1, const void * s2, size_t n) 將s2中連續(xù)n個字節(jié)的數(shù)據(jù)拷貝到s1中 ,注意s1和s2內(nèi)存區(qū)域不能重疊
2. void * memmve(void * s1, const void * s2, size_t n) 將s2中連續(xù)n個字節(jié)的數(shù)據(jù)拷貝到s1中 ,但s1和s2內(nèi)存區(qū)域可以重疊
3. int memcmp(const void * s1, const void * s2, size_t n) 比較內(nèi)存區(qū)域s1和s2的前n個字節(jié),<、=、>分別返回負值、0、正值。
4. void * memchr(const void * s, int c, size_t n) 返回指向s1對象的前n個字節(jié)查找出現(xiàn)c的位置的指針,沒有返回NULL
5. void * memset(void * s, int c, size_t n) 將c拷貝到s1中的前n個字節(jié)中
示例:
int array[5] = {1,4,3,5,2}; for(int i = 0; i < 5; i++) cout<<array[i]<<" "; cout<<endl; memset(array,0,5*sizeof(int)); for(int k = 0; k < 5; k++) cout<<array[k]<<" "; cout<<endl; 輸出的結(jié)果就是: 1 4 3 5 2 0 0 0 0 0
(五)其他函數(shù)
1. char * strerror(int errornum) 返回與errornum匹配的字符串指針
2. size_t strlen(const char * s) 計算字符串s的長度,返回終止符NULL前的字符個數(shù)