当前位置:网站首页>4.字符串(倒序且大小写转换)
4.字符串(倒序且大小写转换)
2022-06-22 15:02:00 【小木荣】
4 字符串
编写一个程序,实现字符串大小写的转换并倒序输出。要求如下:
(1)使用for循环将字符串“HelloWorld”从最后一个字符开始遍历。
(2)遍历的当前字符如果是大写字符,就使用toLowerCase()方法将其转换为小写字符,反之则使用toUpperCase()方法将其转换为大写字符。
(3)定义一个StringBuilder对象,调用append()方法依次添加遍历的字符,最后调用StringBuider对象的toString()方法,并将得到的结果输出。
public class Main {
public static void main(String[] args) {
String str = "HellowWorld";
StringBuffer sb = new StringBuffer();
char[] ch = str.toCharArray();
for (int i = str.length() - 1; i >= 0; i--) {
if (ch[i] >= 'a' && ch[i] <= 'z') {
sb.append(String.valueOf(ch[i]).toUpperCase());//法一
}
if (ch[i] >= 'A' && ch[i] <= 'Z') {
sb.append(str.toLowerCase().toCharArray()[i]);//法二
}
}
System.out.print(sb.toString());
}
}
边栏推荐
- Alibaba cloud middleware's open source past
- Runtime——探索类,对象,分类本质
- SAP ABAP 报告编程-08
- 中信建投证券是跟启牛学堂存在什么关系?开证券账户安全吗
- Simulation Implementation of string
- Ironsource Luna offers a limited time discount for Apple search ads and enjoys 3 months of free service upon registration
- 【山大会议】注册页的编写
- Gbase "library" special training of innovation and application Committee of Beijing fintech Industry Alliance
- [Shanda conference] establishment of webrtc tools for multi person video call
- Unity game optimization (version 2) learning record 8
猜你喜欢

Gbase "library" special training of innovation and application Committee of Beijing fintech Industry Alliance

让pycharm项目里面的文本模板支持jinjia2语法

ironSource Luna 推出苹果搜索广告限时优惠,注册即享3个月免费服务

静态断言 static_assert

84.(cesium篇)cesium模型在地形上运动

Linux安装mysql

The odoo system sets priorities for the views independently developed by the original model

【山大会议】应用设置模块

程序替换函数

Simulation of vector
随机推荐
#进程地址空间
84.(cesium篇)cesium模型在地形上运动
SAP ABAP 子屏幕教程:在 SAP 中调用子屏幕-010
十九、Xv6上下文切换(上下文切换的实现;状态机的封装与恢复)
Alibaba cloud middleware's open source past
[Shangshui Shuo series] day three - VIDEO
String的模拟实现
Research on ICT: domestic databases focus on the ICT market, and Huawei Gauss is expected to become the strongest
(pytorch advanced path 2) word embedding and position embedding
SAP ABAP 中的模块化:宏、子程序和功能模块 -04
Discourse 新用户可插入媒体的数量
Odoo local document function development record
What is the relationship between CSC securities and qiniu school? Is it safe to open a securities account
Recommend several AI Intelligent Platforms
Wechat applet avatar pendant production
Cross border integration, creativity and innovation to help improve the influence of cultural tourism night tour
校企联合在路上!华为云GaussDB又来高校啦
Unity game optimization (version 2) learning record 8
Rosbag use command
Solve the problem of MySQL remote login permission