<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          C語言中字符串操作

          作者: 時間:2016-11-27 來源:網(wǎng)絡(luò) 收藏
          本篇文章是對作進行了詳細(xì)的總結(jié)分析,需要的朋友參考下

          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ù)在已處理的字符串之后填補零結(jié)尾符

          本文引用地址:http://www.ex-cimer.com/article/201611/322538.htm

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

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

          4)函數(shù)原型
          原型:strcpy(char destination[], const char source[]);
          功能:將字符串source拷貝到字符串destination中
          例程:
          #include
          #include
          void main(void)
          {
            char str1[10] = { "TsinghuaOK"};
            char str2[10] = { "Computer"};
            cout <}

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


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

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

          #include
          #include
          void main(void)
          {
            char str1[10] = { "Tsinghua "};
            char str2[10] = { "Computer"};
            cout <}

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

          原型:strcat(char target[], const char source[]);
          功能:將字符串source接到字符串target的后面
          例程:
          #include
          #include
          void main(void)
          {
            char str1[] = { "Tsinghua "};
            char str2[] = { "Computer"};
            cout <}

          運行結(jié)果:Tsinghua Computer

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

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

          #include
          #include
          void main(void)
          {
            char str1[] = { "Tsinghua "};
            char str2[] = { "Computer"};
            cout <}

          運行結(jié)果:Tsinghua Com


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

          #include
          #include
          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"<  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"<}

          運行結(jié)果是:Buffer 2 is less than buffer 1
          Buffer 2 is greater than buffer 3


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

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

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

          注意:strlen函數(shù)的功能是計算字符串的實際長度,不包括看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();