c++ - 将字符串中的小写字母转换为大写字母
问题描述
印刷单元的新打字员正在粗心地打字分配的工作。打字员本应以大写字母输入所有字符,但也输入了小写字母。你的职责是验证所有字符是否都是大写的,如果不是,就这样做。另外,通知打字员犯了多少错误。
输入
bEGIN
输出
BEGIN 1
在某些情况下我得到错误的答案请帮助我是初学者
n=字符串长度
1<=n<=50
int main() {
string s;
cin >> s;
int ans = 0;
for (auto &c : s) {
if (islower(c)) {
ans++;
c = toupper(c);
}
}
cout << s;
cout << endl;
cout << ans;
return 0;
}
解决方案
我认为它也包含空格,所以不要使用>>
operator use getline(cin,string)
, as>>
会在出现空格时终止。
#include <iostream>
using namespace std;
int main() {
string s;
getline(cin,s);
int ans = 0;
for (auto &c : s) {
if (islower(c)) {
ans++;
c = toupper(c);
}
}
cout << s;
cout << endl;
cout << ans;
return 0;
}
这可能是其他测试用例的解决方案。
推荐阅读
- apache - 如何在没有数据的情况下恢复包含静态区域和动态区域的集群配置
- css - 如何在 div 内部缩放而不在 mozilla 浏览器上扩展 div 本身?
- vba - 离线时在收到的电子邮件上运行宏
- javascript - 带有无条件分配错误的Javascript While循环条件
- json - 需要 JSON Schema 条件,具体取决于值
- c - 使用 memcpy 将位域结构复制到数组中
- html - 为什么灰色在不同的浏览器上显示不同?
- android - 改造回调之间的区别
- .net-core - 将附加参数传递给 .csproj 文件中的 npm run
- sql - 连接两个表时如何选择其中一个有条件的两列?