返回列表 發帖

C++ 字串輸入 (包含空白)

本帖最後由 tonyh 於 2022-12-8 19:44 編輯
  1. #include<bits/stdc++.h>
  2. using namespace std;

  3. string str;

  4. int main()
  5. {
  6.     cin.tie(0);
  7.     cin.sync_with_stdio(0);

  8.     getline(cin, str);

  9.     cout<<str<<endl;

  10.     cout<<str.size()<<endl;

  11.     cout<<str[1]<<endl;
  12.     cout<<str[3]<<endl;

  13.     return 0;
  14. }
複製代碼
  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<<endl;
  11.     system("pause");
  12.     return 0;   
  13. }
複製代碼

返回列表