当前位置:网站首页>字符串变形(字符串大小写切换和变现)
字符串变形(字符串大小写切换和变现)
2022-06-25 21:50:00 【你若不离不弃,我必生死相依】
补充基本知识:
JAVA中String类提供了转大写的方法toUpperCase()和转小写的方法toLowerCase()
String a = "ABC";
system.out.println(a.toLowerCase());//abc
String b = "abc";
system.out.println(b.toUpperCase());//ABC
习题描述:
描述
对于一个长度为 n 字符串,我们需要对它做一些变形。
首先这个字符串中包含着一些空格,就像"Hello World"一样,然后我们要做的是把这个字符串中由空格隔开的单词反序,同时反转每个字符的大小写。
比如"Hello World"变形后就变成了"wORLD hELLO"。
代码:
package com.example.test;
import java.util.Scanner;
public class test {
public static void main(String[] args) {
System.out.println("请输入您需要反转的字段");
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String st = scanner.nextLine();
int len = st.length();
System.out.println("长度为" + len);
String ll= tanstring(st, len);
System.out.println("得到转化后的值是"+ll);
}
}
private static String tanstring(String st, int se) {
String res = "";
String tempStr = "";
for (int i = 0; i < se; i++) {
char c = st.charAt(i);//i 为长度 st为输入内容 charAr 类似获取下标的内容
if (c >= 'a' && c <= 'z') {
tempStr += Character.toUpperCase(c); //java 方法体 大写转化为小写
} else if (c >= 'A' && c <= 'Z') {
tempStr += Character.toLowerCase(c); //java 方法体 小写转化为大写
}
//到此处 上面的方法完成大小写的切换 把每个单词独立出来进行 大小切换
//下面的方法是 顺序切换
//字符串拼接 每次循环结束之后 tempStr的值都是最后获取的 最后获取的值拼接在最前 就可以达到顺序的切换
else {
tempStr = c + tempStr;
//保存上一次循环的值
res = tempStr + res;
tempStr = "";
}
}
res = tempStr + res;
System.out.println("输出的数据是" + res);
return res;
}
}
运行结果则是
请输入您需要反转的字段
Hello Word Yan
长度为14
输出的数据是yAN wORD hELLO
下面提供一个debug的 图片更好的理解 字母的调换
边栏推荐
- How do I project points on a 3D plane- How to project a point onto a plane in 3D?
- What is 5g? What can 5g do? What will 5g bring in the future?
- Programmer weekly (issue 4): the wealth view of programmers
- 2022-2028 global TFT LCD touch screen industry research and trend analysis report
- 不荒唐的茶小程序-规则改动
- C language and the creation and use of database
- Fujilai pharmaceutical has passed the registration: the annual revenue is nearly 500million yuan. Xiangyun once illegally traded foreign exchange
- How to design a complex business system? From the understanding of domain design, cloud native, micro service, and middle platform
- Simple and easy-to-use cache library gcache
- China bed and mattress market status research analysis and development prospect forecast report (2022)
猜你喜欢

2022-2028 global TFT touch screen industry research and trend analysis report

Fujilai pharmaceutical has passed the registration: the annual revenue is nearly 500million yuan. Xiangyun once illegally traded foreign exchange

Beyond natural motion: exploring the discontinuity of video interpolation

2022-2028 global open source cloud storage industry research and trend analysis report

Data governance is easier said than done
What are the debugging methods for nodejs

Wpewebkit debugging MSE playback

2022-2028 global web and browser isolation platform industry research and trend analysis report

聊聊Adapter模式

How to design a complex business system? From the understanding of domain design, cloud native, micro service, and middle platform
随机推荐
2022-2028 global DC linear variable differential transformer (LVDT) industry survey and trend analysis report
Chapter 3 use of requests Library
实战:typora里面如何快捷改变字体颜色(博客分享-完美)-2022.6.25(已解决)
Pycharm 2022.1 EAP 2 release
再突破!阿里云进入Gartner云AI开发者服务挑战者象限
Analysis report on market demand situation and investment direction of China's optical transmission equipment industry from 2022 to 2028
TLog 助力盘古框架实现微服务链路日志追踪
ORACLE - 数据查询
[动态规划]最长回文子串-对于动态转移循环顺序的思考
2022-2028 global industrial touch screen industry research and trend analysis report
Oracle - 基本入门
Flutter 網絡請求封裝之Dio(Cookie管理、添加攔截器、下載文件、异常處理、取消請求等)
Market depth analysis and development strategy consulting report of China's fire equipment market 2022-2028
Initialization process of gstlibav
2022-2028 global extrusion coating and lamination production line industry research and trend analysis report
What are the channels for Internet advertising to gain customers?
OSPF - detailed explanation of GRE tunnel (including configuration command)
How do I project points on a 3D plane- How to project a point onto a plane in 3D?
2022-2028 global selective laser sintering service industry research and trend analysis report
Open source optimized VVC encoder in general scenarios