<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    hello world

    隨筆 - 2, 文章 - 63, 評論 - 0, 引用 - 0
    數(shù)據(jù)加載中……

    c++字符串處理函數(shù)

    1、strcpy(char destination[], const char source[]);

    strcpy:將字符串source拷貝到字符串destination中。
    strcpy函數(shù)應用舉例
    原型: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

    第二個字符串將覆蓋掉第一個字符串的所有內(nèi)容!

    注意:在定義數(shù)組時,字符數(shù)組1的字符串長度必須大于或等于字符串2的字符串長度。不能用賦值語句將一個字符串常量或字符數(shù)組直接賦給一個字符數(shù)組。所有字符串處理函數(shù)都包含在頭文件string.h中。


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

    strncpy:將字符串source中前numchars個字符拷貝到字符串destination中。
    strncpy函數(shù)應用舉例
    原型: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個字符!

    3、strcat(char target[], const char source[]);

    strcat:將字符串source接到字符串target的后面。
    strcat函數(shù)應用舉例
    原型: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

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

    4、strncat(char target[], const char source[], int numchars);

    strncat:將字符串source的前numchars個字符接到字符串target的后面。
    strncat函數(shù)應用舉例:
    原型: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


    5、int strcmp(const char firststring[], const char secondstring);

    strcmp:比較兩個字符串firststring和secondstring。
    strcmp函數(shù)應用舉例
    原型: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

    6、strlen( const char string[] );

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

    #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為你輸入的字符總數(shù)字)

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

    posted on 2009-03-21 22:55 聽風 閱讀(285) 評論(0)  編輯  收藏 所屬分類: C++

    主站蜘蛛池模板: 国产国产人免费人成成免视频| 亚洲人色婷婷成人网站在线观看| 亚洲愉拍一区二区三区| 嫩草视频在线免费观看| 免费无码AV一区二区| 亚洲国产精品高清久久久| 四虎免费大片aⅴ入口| 一区二区免费电影| 亚洲天堂一区二区三区| 亚洲自偷自拍另类图片二区| 亚洲精品无码你懂的网站| 免费看片免费播放| 免费成人激情视频| 久久久久久国产精品免费免费男同| 亚洲成aⅴ人片久青草影院按摩| 亚洲va久久久噜噜噜久久男同| 毛片免费全部免费观看| 永久久久免费浮力影院| 精品久久久久久久久免费影院| 亚洲第一网站免费视频| 久久一区二区三区免费播放| 18禁美女黄网站色大片免费观看| 无码中文在线二区免费| 亚洲一本大道无码av天堂| 亚洲高清在线视频| 亚洲精品无码专区| 国产免费一区二区三区在线观看| 成人免费一区二区无码视频| 精品国产亚洲男女在线线电影 | 亚洲色无码专区一区| 一个人看的免费高清视频日本| 最刺激黄a大片免费网站| 亚洲av再在线观看| 亚洲精品午夜视频| 一级特黄a免费大片| 91成人免费观看网站| 国产gv天堂亚洲国产gv刚刚碰| 亚洲av无码国产综合专区| 国产成人1024精品免费| 男人的好看免费观看在线视频| 亚洲自偷自偷图片|