POJ 1298 字母加密 STL String的使用及审题重要性

本文介绍了一种使用C++处理字符串的方法,特别是如何保持非字母字符不变并将所有字母转换为大写的过程。文章通过一个具体例子说明了如何避免常见的陷阱,并提供了一段完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这题很简单,但由于审题不清WA了很多次,太不应该

Any non-alphabetical character should remain the same, and all alphabetical characters will be upper case.

即只要不是字母就不变,

如下写法错误

if(*it == ' ' || *it == ',') continue;

另外,对C++ String的使用中如何将空格也输入也是一个难点,getline不太好用,这里用了一个C的string作为中间量,借助C函数gets完成

#include <iostream> #include <string>//C++ string using namespace std; int main(){ char ss[105]; string s; while (gets(ss)) { s = ss; if (!s.compare("ENDOFINPUT")) break; if (!s.compare("START") || !s.compare("END")) { s.erase(); continue; } string::iterator it;//string 迭代器 for (it = s.begin(); it != s.end(); it++) {//注意非字母的一律不变,这个陷阱一定小心 /* if(*it == ' ' || *it == ',') continue;*/ if(*it >= 'A' && *it <= 'Z'){ if (*it - 5 >= 'A') { *it = *it - 5; } else *it = *it - 5 + 26; } } cout<<s<<endl; //清空s s.erase(); } return 0; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值