关闭。这个问题需要debugging details .它目前不接受答案。
想改进这个问题?将问题更新为 on-topic对于堆栈溢出。
2年前关闭。
Improve this question
The new typist in the printing cell is typing carelessly the jobs assigned. The typist while was supposed to type all the characters in upper case, has got in lower cases too. Your duty is to verify if all the characters are in upper case and do so if not. Also, notify how many mistakes the typist did.
Input
bEGIN
Output
BEGIN 1
在某些情况下我得到错误的答案请帮助我是初学者
n=length of string
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)使用 getline(cin,string)
,如 >>
发生空白时终止。
#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;
}
这可能是其他测试用例的解决方案。
关于c++ - 将字符串中的小写字母转换为大写字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59543121/