返回列表 發帖

字串處理 (七) - 取得字串長度 2

本帖最後由 葉桔良 於 2023-5-6 20:07 編輯

同上題, 運用 string 變數搭配 size() 函式或 length() 函式, 回傳使用者輸入的字串長度(有幾個字元)

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. using namespace std;

  5. int main()
  6. {
  7.     string str;
  8.     cout<<"請輸入一任意英文字串:";
  9.     getline(cin,str);
  10.     cout<<"本字串的長度為"<<str.length()<<"個字元"<<endl;
  11.     system("pause");
  12.     return 0; //123456
  13. }
複製代碼

返回列表