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

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