OpenJudge NOI 1.7 14:大小写字母互换

【题目链接】

OpenJudge NOI 1.7 14:大小写字母互换

【题目考点】

1. 字符串
2. 大小写转换

'a’的ASCII码是97,'A’的ASCII码是65,同一字母的大小写字母的ASCII码差值为32。小写转大写:减32;大写转小写:加32。

【题解代码】

解法1:使用字符数组
#include <bits/stdc++.h>
using namespace std;
int main()
{
    char s[105];
    cin.getline(s, 105);//读入带空格的字符串 
    int len = strlen(s);
    for(int i = 0; i < len; ++i)
    {
        if(s[i] >= 'a' && s[i] <= 'z')//如果s[i]是小写字母 
            s[i] -= 32;//变为大写 
        else if (s[i] >= 'A' && s[i] <= 'Z')//如果s[i]是大写字母 
            s[i] += 32;//变为小写 
    }
    cout << s;
    return 0;
}
解法2:使用string类 <cctype>函数
#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    getline(cin, s);
    for(int i = 0; i < s.length(); ++i)
    {
        if(islower(s[i]))//如果s[i]是小写字母 
            s[i] = toupper(s[i]);//变为大写 
        else if (isupper(s[i]))//如果s[i]是大写字母 
            s[i] = tolower(s[i]);//变为小写 
    }
    cout << s;
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值