發新話題

C++ Gossip 進階型態 - 《字串》字元陣列(C-style 字串)

C++ Gossip 進階型態 - 《字串》字元陣列(C-style 字串)

在C++中字串的本質是由字元所組成的陣列,並在最後加上一個空(null)字元'\0',例如下面這個程式就是一個"hello"字串的宣告:
char str[] = {'h', 'e', 'l', 'l', 'o', '\0'};


之後您可以直接使用str來代表該字串,例如在文字模式下輸出str字串:
cout << str << endl;


C++是使用空字元來識別一個字元陣列是否表示字串,像上例就可以用來表示一個字串"hello",在C++中您也可以這麼宣告字串:
char str[] = "hello";


在這個例子中,雖然您沒有指定空字元'\0',但是程式會自動加上空字元,而它基本上還是字元陣列,與前一個宣告方式是相同的,您從下面這個程式就可以知道:

#include <iostream>
using namespace std;

int main() {
    char str[] = "hello";

    for(int i = 0; i < (sizeof(str)/sizeof(str[0])); i++) {
        if(str == '\0')
            cout << " null";
        else
            cout << " " << str;
    }
    cout << endl;
   
    return 0;
}
執行結果:
h e l l o null

字串是字元陣列,所以您可以使用陣列的存取方式取出每一個字元,在指定"hello"時表面上雖然只有5個字元,但是最後會加上一個空字元'\0',所以 str[]共使用了6個字元,由於字串的最後一個字元為空字元,所以您可以檢驗這個字元來看看字串是否為空,例如:
if(str[0] == '\0') {
    cout << "字串為空";
}


空字元在作條件判斷時,會被視為0,所以上式還可以這麼寫:
if( ! str[0] ) {
    cout << "字串為空";
}


字串一但指定,它的長度就固定了,也就是字元陣列的長度,所以您由使用者輸入取得字串值時,需注意不要超過字串的長度;從使用者輸入取得字串值只要這麼作就可以了:
char str[80];
cout << "輸入字串:";
cin >> str;
cout << "您輸入的字串為 " << str << endl;


這個程式片段可以取得使用者的字串輸入,所輸入的字串長度不得超過80個字元,80個字元的上限包括空白字元,所以實際上可以輸入79個字元;如果輸入的字元超出所宣告的上限,會發生不可預期的錯誤。

要指定新的字串值給它時,您不能像下面的方式指定:

char str[80];
str = "Just";


而必須要一個字元一個字元的指定至陣列中,並在最後加上空白字元,例如:
char str[80] = {'\0'};
str[0] = 'J';
str[1] = 'u';
str[2] = 's';
str[3] = 't';
str[4] = '\0';
cout << str << endl;


這樣的字元指定方式當然相當的不方便,所以C++提供了字串處理的相關函式,例如:
strcpy(str1, str2);  // str2字串複製給str1字串
strcat(str1, str2);  // str2字串串接在str1字串後
strlen(str);  // 計算不含空字元的字串長度
strcmp(str1, str2); // 比較兩個字串


事實上這是從Standard C延續下來的字串處理函式,要使用這些函式,您要含入cstring或是string,例如:
#include <cstring>


下面這個程式是個簡單的示範:

#include <iostream>
#include <cstring>
using namespace std;

int main() {
    char str1[80] = {'\0'};
    char str2[] = "caterpillar";

    cout << "str1: " << str1 << endl
         << "str2: " << str2 << endl
         << endl;

    // 將str2複製給str1
    strcpy(str1, str2);
    cout << "str1: " << str1 << endl
         << "str2: " << str2 << endl
         << endl;

    // 將str2接在str1後
    strcat(str1, str2);
    cout << "str1: " << str1 << endl
         << "str2: " << str2 << endl
         << endl;

    cout << "str1長度:" << strlen(str1) << endl
         << "str2長度:" << strlen(str2) << endl
         << endl;

    cout << "str1與str2比較:" << strcmp(str1, str2) << endl
         << endl;

   
    system("pause");
   
    return 0;
}

執行結果:
str1:
str2: caterpillar

str1: caterpillar
str2: caterpillar

str1: caterpillarcaterpillar
str2: caterpillar

str1長度:22
str2長度:11

str1與str2比較:1

strcmp(str1, str2)會比較字串str1與str2的大小,若相同就傳回0,str1大於str2則傳回大於0的值,小於則傳回小於0的值,比較的標準是依字典順序比對,例如若str1大於str2,表示str1在字典中的順序是在str2之後;strlen()所傳回的字串長度值並不包括空字元,這點必須注意。

如果您使用cin來取得使用者的輸入字串,則您會發現輸入字串時中間不能包括空白,如果您想要在輸入字串時包括空白,您必須使用gets()函式,例如:

char str[80];
cout << "輸入字串:";
gets(str);
cout << "輸入的字串:" << str << endl;


使用gets()函式時有一點必須注意,就是它並不會檢查使用者的輸入是否超出字元陣列的長度,使用時必須小心,有的編譯器會提示警告訊息。
: the `gets' function is dangerous and should not be used.


字串的宣告還有指標(Pointer)的宣告方式,這個留待談到指標時再來說明。

TOP

發新話題

本站所有圖文均屬網友發表,僅代表作者的觀點與本站無關,如有侵權請通知版主會盡快刪除。