返回列表 發帖

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

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

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     string str;
  7.     int n1,n2;
  8.     cout<<"輸入一字串: ";
  9.     getline(cin,str);
  10.     n1=str.size();
  11.     cout<<"本字串共包含: "<<n1<<" 個字元"<<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 str;
  7.     int n1,n2;
  8.     cout<<"輸入一字串: ";
  9.     getline(cin,str);
  10.     n1=str.size();
  11.     cout<<"本字串共包含: "<<n1<<" 個字元"<<endl;
  12.     system("pause");     
  13.     return 0;   
  14. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int a1,a2;
  7.     string b;
  8.     cout<<"請輸入一英文字串:"<<endl;
  9.     getline(cin,b);
  10.     a1=b.size();
  11.     a2=b.length();
  12.     cout<<"字串長度為"<<a1<<"個字元"<<endl;
  13.     cout<<"字串長度為"<<a2<<"個字元"<<endl;
  14.     system("pause");   
  15.     return 0;
  16. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     string str;
  7.     int n1,n2;
  8.     cout<<"輸入一字串: ";
  9.     getline(cin,str);
  10.     n1=str.size();
  11.     cout<<"本字串共包含: "<<n1<<" 個字元"<<endl;
  12.     system("pause");     
  13.     return 0;   
  14. }
複製代碼
Ivy

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     string str;
  7.     int n1,n2;
  8.     cout<<"輸入一字串: ";
  9.     getline(cin,str);
  10.     n1=str.size();
  11.     cout<<"本字串共包含: "<<n1<<" 個字元"<<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 str;
  7.     int n1,n2;
  8.     cout<<"輸入一字串: ";
  9.     getline(cin,str);
  10.     n1=str.size();
  11.     cout<<"本字串共包含: "<<n1<<" 個字元"<<endl;
  12.     system("pause");     
  13.     return 0;   
  14. }
複製代碼
Allen

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     string str;
  7.     int n1,n2;
  8.     cout<<"輸入一字串: ";
  9.     getline(cin,str);
  10.     n1=str.size();
  11.     cout<<"本字串共包含: "<<n1<<"個字元"<<endl;
  12.     system("pause");     
  13.     return 0;   
  14. }
複製代碼
yee

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     string str;
  7.     int n1,n2;
  8.     cout<<"輸入一字串: ";
  9.     getline(cin,str);
  10.     n1=str.size();
  11.     cout<<"本字串共包含: "<<n1<<" 個字元"<<endl;
  12.     system("pause");     
  13.     return 0;   
  14. }
複製代碼

TOP

返回列表