题目:
1007 - 【入门】统计大写英文字母的个数
题目描述
算算以'.'结束的一串字符中含有多少个大写的英文字母。
输入
输入一串字符(长度不超过80),以'.'结束。
输出
输出一行,即这串字符中大写字母的个数。
样例
输入
PRC,PRC,I'm from China.
输出
8
来源
字符串
标签
链接:1007 - 【入门】统计大写英文字母的个数-东方博宜OJ
讲解:
本题需要读入一行字符串,判断里面有多少大写字母
字符数组做法:
#include<bits/stdc++.h>
using namespace std;
int main(){
char c,s[105];
int sum=0;
cin>>c;
while(c!='.'){//循环到句号
if(c>='A'&&c<='Z'){
sum++;
}
s[i]=c;
cin>>c;
}
cout<<sum;
return 0;
}
字符串做法(官方题解):
#include<bits/stdc++.h>
using namepsace std;
string s;//定义一个字符串
int c=0;
int main(){
getline(cin,s);//读取一行字符串,可以包含空格
for(int i=0;i<s.size();i++){//从0开始,到字符串长度
if(s[i]>='A'&&s[i]<='Z'){
c++;
}
}
cout<<c;
return 0;
}
string数据类型视频讲解: