Board logo

標題: [作業] 陣列 (三) - 宣告字串 1 [打印本頁]

作者: 王瑞喻    時間: 2019-9-6 13:01     標題: [作業] 陣列 (三) - 宣告字串 1

本帖最後由 王瑞喻 於 2020-7-12 17:07 編輯

在C++中,字串可運用字元陣列或字串變數來處理。在利用字元陣列宣告字串的時候要注意它的長度,以免位數不夠造成程式錯誤。事實上,每個字串後面都有一個 '\0' 的字元,例如 "tw" 字串,事實上總共用了 3 Bytes,若為中文字,則每個字要佔兩個字元,這一點要特別注意。
char myName[3] = "tw";
char myName[7] = "王小明";

[attach]7078[/attach]
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {      
  6.     string str="大家好!我叫王小明!";
  7.     cout<<str<<endl;
  8.     char strc[19]="大家好!我叫王小明!";
  9.     cout<<strc<<endl;  
  10.     system("pause");
  11.     return 0;
  12. }
複製代碼

作者: 蔡少宇    時間: 2019-9-18 13:09

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.         char name[7]="蔡少宇";
  7.         cout<<"大家好!我叫"<<name<<"!"<<endl;
  8.         system("pause");
  9.         return 0;
  10. }
複製代碼

作者: 鄭羽捷    時間: 2019-9-20 22:31

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     char name[7]="鄭羽捷";
  7.     cout<<"大家好!我叫"<<name<<"!"<<endl;
  8.     system("pause");
  9.     return 0;       
  10. }
複製代碼

作者: 曾宥程    時間: 2019-9-20 22:34

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.         char name[7]="王小明";
  7.         cout<<"大家好!我叫"<<name<<"!"<<endl;
  8.         system("pause");
  9.         return 0;
  10. }
複製代碼

作者: 黃傳耀    時間: 2019-9-21 10:18

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.    char name[7]="黃傳耀";
  7.    cout<<"大家好!我叫:"<<name<<"!"<<endl;
  8.    system("pause");        
  9.    return 0;        
  10. }
複製代碼

作者: 洪藜芸    時間: 2019-9-21 10:18

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     char name[7]="王小明";
  7.     cout<<"大家好,我叫"<<name<<"!"<<endl;
  8.     system("pause");
  9.     return 0;
  10. }
複製代碼

作者: 王翎璇    時間: 2019-9-21 10:19

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     char name[7]="王翎璇";
  7.     cout<<"大家好!我叫"<<name<<"!"<<endl;  
  8.     system("pause");
  9.     return 0;   
  10. }
複製代碼

作者: 洪子涵    時間: 2019-9-21 10:20

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     char name[7]="王小明";
  7.     cout<<"大家好,我是"<<name<<"!"<<endl;
  8.     system("pause");
  9.     return 0;
  10. }
複製代碼

作者: 郭哲維    時間: 2019-9-21 10:26

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     char name[7]="王曉明";
  7.     cout<<"大家好!我叫"<<name<<"!"<<endl;
  8.     system("pause");
  9.     return 0;
  10. }      
複製代碼

作者: 林孟蓁    時間: 2019-9-29 16:31

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.         char name[7]="王小明";
  7.         cout<<"大家好!我叫"<<name<<"!"<<endl;
  8.         system("pause");
  9.         return 0;
  10. }
複製代碼

作者: 駱顗安    時間: 2020-7-23 19:32

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     string str="大家好!我是王小明!";
  7.     cout<<str<<endl;
  8.     char str2[19]="大家好!我是王小明!";
  9.     cout<<str2<<endl;
  10.     system("pause");
  11.     return 0;
  12. }
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/) Powered by Discuz! 7.2