酷代码 AI
菜单
服务商
DeepSeek字节豆包
更多选项

C++分离大小写字母:ASCII码字母串分类处理实现

根据题目描述,输入有三行,但第三行与第二行内容完全相同,实际上我们只需要处理第二行的字符串。任务是将大小写字母分离,输出大写字母串和小写字母串。 以下是 C++ 解决方案: ```cpp #include <iostream> #include <string> #include <cctype> using namespace std; int main() { int n; string s; // 读取第一行:长度(实际上用不到,因为字符串长度已知) cin >> n; // 读取第二行:混合字符串 cin >> s; // 忽略第三行(与第二行相同) cin.ignore(); string temp; getline(cin, temp); // 读取第三行并丢弃 string upperStr, lowerStr; for (char ch : s) { if (isupper(ch)) { upperStr += ch; } else if (islower(ch)) { lowerStr += ch; } } cout << upperStr << endl; cout << lowerStr << endl; return 0; } ``` ### 代码说明 - `isupper(ch)` 检查字符是否为大写字母 - `islower(ch)` 检查字符是否为小写字母 - 分别将大写和小写字母拼接到对应的字符串中 - 最终输出两个结果字符串 ### 示例运行 输入: ``` 9 WwRaOyNsG WwRaOyNsG ``` 输出: ``` WRONG ways ``` > 注意:示例输出中大写字母串为 `WRONG`,小写字母串为 `ways`,与题目描述一致(大写字母:W,R,O,N,G;小写字母:w,a,y,s)。 ######[AI问答 | 388点数解答 | 2026-07-10 19:23:10]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]