萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 編程語言綜合 >> C語言字符串操作總結大全

C語言字符串操作總結大全

 1)字符串操作 

strcpy(p, p1) 復制字符串 
strncpy(p, p1, n) 復制指定長度字符串 
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) 以目標字符串的所有字符作為集合,在當前字符串查找該集合的任一元素 
strspn(p, p1) 以目標字符串的所有字符作為集合,在當前字符串查找不屬於該集合的任一元素的偏移 
strcspn(p, p1) 以目標字符串的所有字符作為集合,在當前字符串查找屬於該集合的任一元素的偏移  
* 具有指定長度的字符串處理函數在已處理的字符串之後填補零結尾符 

2)字符串到數值類型的轉換 
strtod(p, ppend) 從字符串 p 中轉換 double 類型數值,並將後續的字符串指針存儲到 ppend 指向的 char* 類型存儲。
strtol(p, ppend, base) 從字符串 p 中轉換 long 類型整型數值,base 顯式設置轉換的整型進制,設置為 0 以根據特定格式判斷所用進制,0x, 0X 前綴以解釋為十六進制格式整型,0    前綴以解釋為八進制格式整型
atoi(p) 字符串轉換到 int 整型 
atof(p) 字符串轉換到 double 符點數 
atol(p) 字符串轉換到 long 整型 

3)字符檢查 
isalpha() 檢查是否為字母字符 
isupper() 檢查是否為大寫字母字符 
islower() 檢查是否為小寫字母字符 
isdigit() 檢查是否為數字 
isxdigit() 檢查是否為十六進制數字表示的有效字符 
isspace() 檢查是否為空格類型字符 
iscntrl() 檢查是否為控制字符 
ispunct() 檢查是否為標點符號 
isalnum() 檢查是否為字母和數字 
isprint() 檢查是否是可打印字符 
isgraph() 檢查是否是圖形字符,等效於 isalnum() | ispunct() 

4)函數原型
原型:strcpy(char destination[], const char source[]); 
功能:將字符串source拷貝到字符串destination中 
例程:  
 #include <iostream.h> 
#include <string.h> 
void main(void) 

char str1[10] = { "TsinghuaOK"}; 
char str2[10] = { "Computer"}; 
cout <<strcpy(str1,str2)<<endl; 
}

運行結果是:Computer 
第二個字符串將覆蓋掉第一個字符串的所有內容! 
注意:在定義數組時,字符數組1的字符串長度必須大於或等於字符串2的字符串長度。不能用賦值語句將一個字符串常量或字符數組直接賦給一個字符數組。所有字符串處理函數都包含在頭文件string.h中。


strncpy(char destination[], const char source[], int numchars);

strncpy:將字符串source中前numchars個字符拷貝到字符串destination中。 
strncpy函數應用舉例 
原型:strncpy(char destination[], const char source[], int numchars); 
功能:將字符串source中前numchars個字符拷貝到字符串destination中 
例程: 

#include <iostream.h> 
#include <string.h> 
void main(void) 

char str1[10] = { "Tsinghua "}; 
char str2[10] = { "Computer"}; 
cout <<strncpy(str1,str2,3)<<endl; 
}

運行結果:Comnghua 
注意:字符串source中前numchars個字符將覆蓋掉字符串destination中前numchars個字符!

原型:strcat(char target[], const char source[]); 
功能:將字符串source接到字符串target的後面 
例程: 
#include <iostream.h> 
#include <string.h> 
void main(void) 

char str1[] = { "Tsinghua "}; 
char str2[] = { "Computer"}; 
cout <<strcpy(str1,str2)<<endl; 
}

運行結果:Tsinghua Computer

注意:在定義字符數組1的長度時應該考慮字符數組2的長度,因為連接後新字符串的長度為兩個字符串長度之和。進行字符串連接後,字符串1的結尾符將自動被去掉,在結尾串末尾保留新字符串後面一個結尾符。 

原型:strncat(char target[], const char source[], int numchars); 
功能:將字符串source的前numchars個字符接到字符串target的後面 
例程:

#include <iostream.h> 
#include <string.h> 
void main(void) 

char str1[] = { "Tsinghua "}; 
char str2[] = { "Computer"}; 
cout <<strncat(str1,str2,3)<<endl; 
}

運行結果:Tsinghua Com


原型:int strcmp(const char firststring[], const char secondstring); 
功能:比較兩個字符串firststring和secondstring 
例程: 

#include <iostream.h> 
#include <string.h> 
void main(void) 

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"<<endl; 
else  
cout <<"Buffer 2 is less than buffer 1"<<endl; 
ptr = strcmp(buf2,buf3);  
if(ptr > 0) 
cout <<"Buffer 2 is greater than buffer 3"<<endl; 
else  
cout <<"Buffer 2 is less than buffer 3"<<endl; 
}

運行結果是:Buffer 2 is less than buffer 1  
                  Buffer 2 is greater than buffer 3


原型:strlen( const char string[] ); 
功能:統計字符串string中字符的個數 
例程: 

#include <iostream.h> 
#include <string.h> 
void main(void) 

  char str[100];  
  cout <<"請輸入一個字符串:"; 
  cin >>str; 
  cout <<"The length of the string is :"<<strlen(str)<<"個"<<endl; 
}

運行結果The length of the string is x (x為你輸入的字符總數字)

注意:strlen函數的功能是計算字符串的實際長度,不包括''在內。另外,strlen函數也可以直接測試字符串常量的長度,如:strlen("Welcome")。

  
void *memset(void *dest, int c, size_t count);  
將dest前面count個字符置為字符c.  返回dest的值. 

void *memmove(void *dest, const void *src, size_t count);  
從src復制count字節的字符到dest. 如果src和dest出現重疊, 函數會自動處理.  返回dest的值. 

void *memcpy(void *dest, const void *src, size_t count);  
從src復制count字節的字符到dest. 與memmove功能一樣, 只是不能處理src和dest出現重疊.  返回dest的值. 

void *memchr(const void *buf, int c, size_t count);  
在buf前面count字節中查找首次出現字符c的位置. 找到了字符c或者已經搜尋了count個字節, 查找即停止. 操作成功則返回buf中首次出現c的位置指針, 否則返回NULL. 

void *_memccpy(void *dest, const void *src, int c, size_t count);  
從src復制0個或多個字節的字符到dest. 當字符c被復制或者count個字符被復制時, 復制停止. 

如果字符c被復制, 函數返回這個字符後面緊挨一個字符位置的指針. 否則返回NULL. 

int memcmp(const void *buf1, const void *buf2, size_t count);  
比較buf1和buf2前面count個字節大小.  
返回值< 0, 表示buf1小於buf2;  
返回值為0, 表示buf1等於buf2;  
返回值> 0, 表示buf1大於buf2. 

int memicmp(const void *buf1, const void *buf2, size

copyright © 萬盛學電腦網 all rights reserved