標題:
字串處理 (六) - 取得字串長度 1
[打印本頁]
作者:
方浩葦
時間:
2024-10-5 13:42
標題:
字串處理 (六) - 取得字串長度 1
本帖最後由 方浩葦 於 2024-10-5 18:11 編輯
設計一程式, 能計算使用者輸入的字串長度(有幾個字元)
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int sum=0;
char str[50];
cout<<"輸入一字串: ";
cin.getline(str,50);
for (int i = 0; str[i] != '\0'; i++)
sum++;
cout<<"本字串共包含: "<<sum<<" 個字元"<<endl;
system("pause");
return 0;
}
複製代碼
作者:
鄭豊翰
時間:
2024-11-30 10:14
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string str;
int sum=0;
cout << "輸入一字串: ";
getline(cin, str);
for (int i = 0; str[i] != '\0'; i++)
sum++;
cout<<"本字串共包含: "<<sum<<" 個字元"<<endl;
system("pause");
return 0;
}
複製代碼
作者:
劉冠毅
時間:
2024-12-7 14:00
#include<iostream>
#include<cstdlib>
#include<string>
#include <algorithm>
using namespace std;
int main()
{
int sum=0;
char str[50];
cout<<"輸入字串: ";
cin.getline(str,50);
for(int i =0;str[i]!='\0';i++){
sum=sum+1;
}
cout<<"此字串有 "<<sum<<"個字元"<<endl;
system("pause");
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2