當前位置:才華齋>計算機>C語言>

C語言超詳細字串操作總結

C語言 閱讀(1.68W)

本文是本站小編搜尋整理的關於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函式的功能是計算字串的實際長度,不包括'