返回列表 發帖

字串處理 (一)

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

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

試分別利用宣告字串與宣告字元陣列的方式, 在銀幕上顯示 "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. }
複製代碼
Su Wa

  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. #include<cmath>
  4. using namespace std;
  5. int main()
  6. {
  7.     string str1="Good morning!";
  8.     char str2[14]="Good morning!";
  9.     cout<<str1<<endl;
  10.     cout<<str2<<endl;
  11.     cout<<str1[0]<<endl;
  12.     cout<<str2[0]<<endl;
  13.     system("pause");
  14.     return 0;  
  15. }
複製代碼
Vincent

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. string str1="Good morning!";
  2.     char str2[14]="Good morning!";
  3.     cout<<str1<<endl;
  4.     cout<<str2<<endl;
  5.     cout<<str1[0]<<endl;
  6.     cout<<str2[0]<<endl;
  7.     system("pause");
  8.     return 0;   
  9. }
複製代碼

TOP

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

TOP

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

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]<<str1[1]<<str1[2]<<str1[3]<<endl;
  11.     cout<<str2[5]<<str2[6]<<str2[7]<<str2[8]<<str2[9]<<str2[10]<<str2[11]<<str2[12]<<str2[13]<<endl;
  12.     system("pause");
  13.     return 0;
  14. }
複製代碼
Jian-wei Wang

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

返回列表