编写一个程序,实现字符串大小写的转换并倒序输出用Java
时间: 2024-04-30 15:18:35 浏览: 120
java的倒序排列
以下是一个示例代码,可以将输入的字符串进行大小写转换并倒序输出:
```java
import java.util.Scanner;
public class StringCaseConverter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个字符串:");
String input = scanner.nextLine();
String converted = convertCase(input);
String reversed = reverseString(converted);
System.out.println("转换后的字符串:" + reversed);
}
public static String convertCase(String input) {
char[] chars = input.toCharArray();
for (int i = 0; i < chars.length; i++) {
if (Character.isUpperCase(chars[i])) {
chars[i] = Character.toLowerCase(chars[i]);
} else {
chars[i] = Character.toUpperCase(chars[i]);
}
}
return new String(chars);
}
public static String reverseString(String input) {
char[] chars = input.toCharArray();
for (int i = 0; i < chars.length / 2; i++) {
char temp = chars[i];
chars[i] = chars[chars.length - 1 - i];
chars[chars.length - 1 - i] = temp;
}
return new String(chars);
}
}
```
使用示例:
```
请输入一个字符串:Hello World!
转换后的字符串:!DLROW OLLEh
```
阅读全文