輸入:字符串
輸出:大寫字母的個數
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
getline(cin,str);
int count = 0;
if (str == "")
cout << 0<<endl;
else
{
for (int i = 0;i < str.size();i++)
{
if (str[i]>='A' && str[i]<='Z')
{
count++;
}
}
cout << count << endl;
}
return 0;
}
這道題目很簡單,不過這裏需要判斷字符串爲空的情況(輸出0),用到了getline(cin,str).