返回列表 發帖

認識字串

我們可運用 length() 函式帶出字串的長度,而從每個索引位置帶出來的單一元素都是字元。

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     string str="Hello World!";
  7.     int len=str.length();
  8.     cout<<"字串:"<<str<<endl;
  9.     cout<<"長度:"<<len<<endl;
  10.     cout<<"字串中的每個字元:"<<endl;
  11.     for(char c: str)
  12.         cout<<c<<endl;
  13.     /*
  14.     for(int i=0; i<len; i++)
  15.         cout<<str[i]<<endl;
  16.     */
  17.     system("pause");
  18.     return 0;
  19. }
複製代碼
istak.teach2@gmail.com

返回列表