C++ 入門指南 4.01
練習 15.1 參考程式 - 練習字元陣列
// 引入標準程式庫中相關的輸入、輸出程式
#include <iostream>
// cout 為 std 中的輸出物件
using std::cout;
// endl 為 std 中的斷行符號
using std::endl;
// string 為 std 中字串型態
using std::string;
int main(void) {
// 宣告建立字元陣列變數
char text[] = {'h', 'e', 'l', 'l', 'o', '\0'};
// 宣告建立字串變數
string text2 = "hello";
// 比較字元陣列與字串的內容是否一樣
if (text == text2) {
cout << "equal" << endl;
}
else {
cout << "not equal" << endl;
}
// 最後回傳 0 給作業系統
return 0;
}
/* 《程式語言教學誌》的範例程式
http://kaiching.org/
檔名:exercise1501.cxx
編譯:g++ exercise1501.cxx
執行:./a.out
功能:C++入門指南單元十五的練習
作者:張凱慶 */
回到練習題目