C++ 進階 教學 : C 與 C++ 的字串運算

C++ 進階 教學 : C 與 C++ 的字串運算



好的頻道需要你的支持,謝謝你使用Youtube影片的超級感謝功能給我們鼓勵。






















好的頻道需要你的支持,謝謝你使用Youtube影片的超級感謝功能給我們鼓勵。

字串

字元串列。也就是大量字元的集合。在 C 語言時期以字元陣列字元指標來做為字串型別,在 C++ 文件中常以 C-style string 稱之。在 C++ 時期採用 string 類別為字串型別。
  1. 字元陣列與字元指標
    • 宣告字元陣列
      • char name[length + 1];
      • 字元陣列最後一個元素必須為空字元,其字元碼為 0 ,代碼為 NUL。
      • char name[length + 1] = {'1','2','3',.....};
      • 未指派的空間會置入空字元
      • char name[length+1] = "12345";
      • 未指派的空間會置入空字元
      • char name[] = "12345";
      • 執行環境會自動在最末端串上空字元

    • 宣告字元指標
      • char * name;
      • char * name = "12345";  //不建議
      • 沒有 new 就不能 delete 。
      • char * name = new char[length + 1];

  2. C 的 cstring 中的函式
    • strlen
    • size_t strlen ( const char * str );
      取得 str 的長度

    • strcpy
    • char * strcpy ( char * destination, const char * source );
      將 source 的值複製給 destination。
      在 C 語言時期,還不能用 '=' 指派字串。是用這個函式,進行指派的運算。
      • 字元陣列
      • 字元指標
      • 記得要 new 和 delete。

    • memcpy
    • void * memcpy ( void * destination, const void * source, size_t num );
      將 soruce 中 num 長度的內容,複製到 destination 。
      用 strcpy 複製時,若 source 的長度小於 destination,則 destination 中多出來的長度中的內容並不會保留。
      若希望能保留 destination 中多出來的長度的內容,必須使用 memcpy 。
      • 不同字串
      • 同一字串
      • 不同型態

    • strcat
    • char * strcat ( char * destination, const char * source );
      將 source 的內容,串接在 destination 原先內容的後面。

    • strcmp
    • int strcmp ( const char * str1, const char * str2 );
      從第一個字元開始逐一比對 str1 與 str2 的所有字元。
      比對過程若有不一樣的字元時:
      * 若 str1 的字元大於 str2 的字元,運算的結果為正值。
      * 若 str1 的字元小於 str2 的字元,運算的結果為負值。
      若 str1 的全部字元等於str2 的全部字元,運算的結果為零。

    • const char * strstr ( const char * str1, const char * str2 );
    • 運算出 str2 在 str1 中第一次出現的位置。若不存在結果為 NULL。

    • char * strtok ( char * str, const char * delimiters );
    • 依照 delimiters 的內容切割 str。delimiters 中的各個字元為或的關係。str 的內容會被改變。

  3. C++ 的 string 中的 string 類別
    • 物件變數
      • 宣告
      • 指派
    • 物件指標
      • 宣告
      • 指派
    • 建構函式
    • 成員函式
      • char& at (size_t pos); / [ ]
      • size_t size() const; / size_t length() const;
      • 字串物件的長度
      • string& assign (const string& str); / =
      • 指派新內容給物件,物件中的舊內容會被取代。
      • string& append (const string& str); / +=
      • 將 str 串接到物件目前內容的最後面。
      • string& insert (size_t pos, const string& str);
      • 在物件目前內容 pos 的位置,插入 str 。
      • string& erase (size_t pos = 0, size_t len = npos);
      • 把物件目前內容中從 pos 開始,長度 len 的內容抺除。
      • string& replace (size_t pos, size_t len, const string str);
      • 用 str 取代物件目前內容中,從 pos 開始長度 len 的內容。
      • size_t find (const string& str, size_t pos = 0) const;
      • 從 pos 開始,找出 str 第一次出現的位置。若不存在,值為 npos(-1) 。
      • size_t find_first_of (const string& str, size_t pos = 0) const;
      • 從 pos 開始,找出 str 中的任何一個字元,第一次出現的位置。若不存在,值為 npos(-1) 。
      • string substr (size_t pos = 0, size_t len = npos) const;
      • 運算出一個新的 stirng 物件,其內容為本物件籨 pos 開始長度 len 的內容。
      • int compare (const string& str) const;
      • 與 str 比大小。正值、零、負值分別代表大於、等於、小於。

  4. string 標頭檔中與字串運算相關的函式
    • Dev C++ 中使用 C++11的函式
    • 重要函式
      • int stoi (const string&  str, size_t* idx = 0, int base = 10);
      • 字串轉整數
      • string to_string (int val);
      • 整數轉字串

  5. 常見字串運算範例
    • 字元數量統計
    • 接收輸入下列文字:
      1. 1GL 1GL means First Generation Programming Language. It is actually a machine level programming language that is written in one's and zero's. These are the programs that can be understood by the computer's central processing unit directly. There's no need to run it into a compiler or any other programming software. There are also second, third, up to fourth generation computer programming language. 2. Abandon ware Abandon ware are software that are not being used, supported, or sold by its respective publisher any longer. These types of software cannot be resold or redistributed to end-users unless otherwise given away as a freeware. 3. ActiveX ActiveX are programs using the Visual Basic programming language. It is basically created for active contents of the software that are commonly used by other applications that are already installed or existing on the computer. Sometimes, it even shares the ones used by the operating system itself. ActiveX is the one responsible for the interactive features of web-based programs..

      分別統計出大寫字母, 小寫字母, 數字, 空格及其他符號各有幾個.
      例:
      大寫: 1 個
      小寫: 2 個
      數字: 3 個
      空格:  4 個
      其他符號: 5 個

    • 登入
    • 接受輸入帳號及密碼。若帳號為"abcd",密碼為"1234",顯示"歡迎光臨",否則顯示"帳號或密碼錯誤,請重新輸入"。但若錯誤三次,則顯示"錯誤己逹三次",並結束程式。

    • 身份證字號
    • 接收輸入一個身份證字號,檢查是否符合下列規則:
      * 共十個字元
      * 第一個字元必須是大寫英文字母。
      * 第二個字元必須是 1 或 2。
      * 第三個字元至第十個字元必須是數字字元。
      * 第一個英文字元依照下方英文字母數字代號表格來取得數字代號。
      * 用數字代號取代原英文字母,得到一個 11 位數之數字。
      * 將 11 位數的數字拆開成 11 個數字,並分別乘以
      1,9,8,7,6,5,4,3,2,1,1。
      * 將上述步驟得到的相乘後的 11 個數字相加。
      * 若上述步驟得到的加總後的數字是 10 的倍數,即為正確的身份證字號
      若格式正確,輸出正確,否則輸出錯誤。

英文字母數字代號表


英文字母
數字代號
10
11
12
13
14
15
16
17
18
19
英文字母
數字代號
20
21
22
23
24
25
26
27
28
29
英文字母
數字代號
30
31
32
33
34
35
PS 請注意順序並非完全依照英文字母順序。


作者 : 許裕永