当前位置:网站首页>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());
}
}
边栏推荐
猜你喜欢

静态断言 static_assert

B树和B+树

Quickly play ci/cd graphical choreography

推進兼容適配,使能協同發展 GBase 5月適配速遞

Devsecops: best practices for ci/cd pipeline security

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

webDriver以及Selenium使用总结
New design of databend SQL planner

Dear students, don't read the textbooks any more. Just read this one for the complexity of time

ArcGIS JS之 4.23之IIS本地部署与问题解决
随机推荐
SAP ABAP 中的 Smart Forms-014
Unity game optimization (version 2) learning record 8
在JFlash中添加未知类型的单片机
Quickly play ci/cd graphical choreography
SAP ABAP 表控制与示例-07
数睿数据荣获第二届ISIG中国产业智能大会两项年度大奖
Alibaba cloud middleware's open source past
华为云HCDEZ专场暨分布式技术峰会:华为云分布式云原生技术与实践之路
Applet development - Custom expiration cache
Merge sort of sorting
Bridging the gap between open source databases and database services
Runtime——探索类,对象,分类本质
odoo本地文档功能开发记录
[single chip microcomputer] [make buzzer sound] know the buzzer and let it make the sound you want
中信建投证券是跟启牛学堂存在什么关系?开证券账户安全吗
C language learning -17- function is passed in as a parameter
ORB_VI思想框架
Discourse 的信任级别
Application of mongodb in Tencent retail premium code
[Shanda conference] software performance optimization and bug repair