博客
关于我
ALGO-84 大小写转换
阅读量:87 次
发布时间:2019-02-26

本文共 646 字,大约阅读时间需要 2 分钟。

C++程序示例:字符串大小写转换

以下是一个简单的C++程序示例,功能是将字符串中的字符转换为大小写交替。程序通过读取用户输入的字符串,并对每个字符进行处理。

代码结构如下:

#include 
#include
using namespace std;int main(void) { string s; cin >> s; int length = s.length(); int c = 'A' - 'a'; for(int i = 0; i < length; i++) { if(s[i] > 'Z' + 1) { s[i] -= c; } else { s[i] += c; } } cout << s << endl;}

程序的主要功能是对输入的字符串进行字符转换。如果字符是大写字母,则转换为小写字母;如果是小写字母,则转换为大写字母。具体转换逻辑如下:

  • 读取输入字符串
  • 定义转换字符'c'为'A' - 'a',即转换为一个固定的位移值
  • 遍历字符串中的每个字符
  • 对于每个字符,检查其是否为大写字母
    • 如果是大写字母,则减去'c',将其转换为小写字母
    • 否则,将其加上'c',将其转换为大写字母
  • 最后输出处理后的字符串
  • 这个程序适用于需要对字符串进行简单字符转换的场景,例如文本文件的格式转换或自动化处理任务。

    转载地址:http://mrhu.baihongyu.com/

    你可能感兴趣的文章
    NOTE:rfc5766-turn-server
    查看>>
    Notepad ++ 安装与配置教程(非常详细)从零基础入门到精通,看完这一篇就够了
    查看>>
    Notepad++在线和离线安装JSON格式化插件
    查看>>
    notepad++最详情汇总
    查看>>
    notepad++正则表达式替换字符串详解
    查看>>
    notepad如何自动对齐_notepad++怎么自动排版
    查看>>
    Notes on Paul Irish's "Things I learned from the jQuery source" casts
    查看>>
    Notification 使用详解(很全
    查看>>
    NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
    查看>>
    NotImplementedError: Could not run torchvision::nms
    查看>>
    nova基于ubs机制扩展scheduler-filter
    查看>>
    Now trying to drop the old temporary tablespace, the session hangs.
    查看>>
    nowcoder—Beauty of Trees
    查看>>
    np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
    查看>>
    np.power的使用
    查看>>
    NPM 2FA双重认证的设置方法
    查看>>
    npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
    查看>>
    npm build报错Cannot find module ‘webpack‘解决方法
    查看>>
    npm ERR! ERESOLVE could not resolve报错
    查看>>
    npm ERR! fatal: unable to connect to github.com:
    查看>>