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

标签 c++ string

关闭。这个问题需要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/

相关文章:

python - 仅当字符串与其他字符间隔开时,如何检查行中的确切字符串?

php - 检查查询返回的输出是否包含字符串/数字

python - 如何在 python 中将 001 之类的数字反转为 100?

c++ - 格式化和查找文本 C++

java - 字符串和运行时错误。 (颠倒字符串中字符顺序的程序)

c++ - 使用C++动态分配多维结构数组

c++ - 从 Julia 调用 C/C++

c++ - C++中如何选择同名函数

c++ - 在 C++ 中释放指针的 std::vector 的正确方法是什么?

c++ - 在未初始化局部变量的情况下,Windows 线程堆栈保护页面机制如何工作?