返回列表 發帖

字串處理 (一)

我們在宣告字元陣列的時候要注意它的長度,以免位數不夠造成程式錯誤。事實上,每個字串後面都有一個 '\0' 的字元,例如 "tw" 字串,事實上總共用了 3 Bytes,若為中文字,則每個字要佔兩個字元

例如:
char str[3]="tw";
char str[9]="字串處理"

補充:‘\0’ 是 c/c++ 語言中的字符串結束符,在ASCII字符集中對應空字符NULL

試分別利用宣告字串與宣告字元陣列的方式, 在銀幕上顯示 "Good morning!" 字樣.
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     string str1="Good morning!";
  7.     char str2[14]="Good morning!";
  8.     cout<<str1<<endl;
  9.     cout<<str2<<endl;
  10.     cout<<str1[0]<<endl;
  11.     cout<<str2[0]<<endl;
  12.     system("pause");
  13.     return 0;   
  14. }
複製代碼

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     string str1="Good morning!";
  7.     char str2[14]="Good morning!";
  8.     cout<<str1<<endl;
  9.     cout<<str2<<endl;
  10.     system("pause");
  11.     return 0;   
  12. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     string str1="Good morning!";
  7.     char str2[14]="Good morning!";
  8.     cout<<str1<<endl;
  9.     cout<<str2<<endl;
  10.     cout<<str1[0]<<endl;
  11.     cout<<str2[0]<<endl;
  12.     system("pause");
  13.     return 0;   
  14. }
複製代碼

TOP

本帖最後由 蔡沛倢 於 2023-9-15 20:44 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     char a[5]="abcd";
  7.     cout<<"原本:"<<a<<endl;
  8.     cout<<"拆解:"endl;
  9.     for(int i=0;i<=3;i++)
  10.     {
  11.        cout<<a[i]<<endl;
  12.     }
  13.     system("pause");
  14.     return 0;
  15. }
複製代碼

TOP

  1. #include<cstdlib>
  2. #include<iostream>
  3. using namespace std;
  4. int main(){
  5.     string str1="Good morning!";
  6.     char str2[14]="Good morning!";
  7.     cout<<str1<<endl;
  8.     cout<<str2<<endl;
  9.     cout<<str1[0]<<endl;
  10.     cout<<str2[0]<<endl;
  11.     system("pause");
  12.     return 0;
  13. }
複製代碼
Attention Seeker </3

TOP

4

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     string str1="Good morning!";
  7.     char str2[14]="Good morning!";
  8.     cout<<str1<<endl;
  9.     cout<<str2<<endl;
  10.     cout<<str1[0]<<endl;
  11.     cout<<str2[0]<<endl;
  12.     system("pause");
  13.     return 0;   
  14. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     string str1="This boy is good cookie!";
  7.     char str2[25]="This boy is good cookie!";
  8.      cout<<str1<<endl;
  9.     cout<<str2<<endl;
  10.     cout<<str1[0]<<endl;
  11.     cout<<str2[0]<<endl;
  12.    
  13.    
  14.     system("pause");
  15.     return 0;   
  16. }
複製代碼

TOP

6

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     string str1="Good morning!";
  7.     char str2[14]="Good morning!";
  8.     cout<<str1<<endl;
  9.     cout<<str2<<endl;
  10.     cout<<str1[0]<<endl;
  11.     cout<<str2[0]<<endl;
  12.     system("pause");
  13.     return 0;
  14. }
複製代碼

TOP

  1.     #include<iostream>
  2.     #include<cstdlib>
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         string str1="Good morning!";
  7.         char str2[14]="Good morning!";
  8.         cout<<str1<<endl;
  9.         cout<<str2<<endl;
  10.         cout<<str1[0]<<endl;
  11.         cout<<str2[0]<<endl;
  12.         system("pause");
  13.         return 0;   
  14.     }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     string talk="Beans!";
  7.     char talk2[7]="Beans!";
  8.     cout<<talk<<endl;
  9.     cout<<talk2<<endl;
  10.     cout<<talk[0]<<endl;
  11.     cout<<talk2[0]<<endl;
  12.     system("pause");
  13.     return 0;   
  14. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     string str1="Good morning!";
  7.     char str2[14]="Good morning!";
  8.     cout<<str1<<endl;
  9.     cout<<str2<<endl;
  10.     cout<<str1[0]<<endl;
  11.     cout<<str2[0]<<endl;
  12.     system("pause");
  13.     return 0;   
  14. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     string str1="Good morning!";
  7.     char str2[14]="Good morning!";
  8.     cout<<str1<<endl;
  9.     cout<<str2<<endl;
  10.     cout<<str1[0]<<endl;
  11.     cout<<str2[0]<<endl;
  12.     system("pause");
  13.     return 0;   
  14. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     string str1="what hall";
  7.     char str2[]="what hall";
  8.     cout<<str1<<endl;
  9.     cout<<str2<<endl;
  10.     cout<<str1[2]<<endl;
  11.     cout<<str2[3]<<endl;
  12.     system("pause");
  13.     return 0;   
  14. }
複製代碼

TOP

返回列表