将字符串中的小写字母转换为大写字母

问题描述 投票:0回答:1

打印单元中的新打字员正在粗心输入分配的作业。打字员本来应该用大写字母键入所有字符,但也要用小写字母键入。您的职责是验证所有字符是否都大写,如果不是,请这样做。另外,通知打字员犯了多少错误。

输入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;
 }
c++ string
1个回答
0
投票

我认为它也包含空格,所以不要使用>>运算符,而要使用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) && c!=' ') {
         ans++;
         c = toupper(c);
     }
 }
 cout << s;
 cout << endl;
 cout << ans;
 return 0;
}

这可能是其他测试用例的解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.