当前位置:网站首页>4. string (reverse order and case conversion)

4. string (reverse order and case conversion)

2022-06-22 16:18:00 Xiaomurong

4 character string
Write a program , Realize the conversion of string case and output in reverse order . Requirements are as follows :
(1) Use for Loop the string “HelloWorld” Start traversing from the last character .
(2) If the current character traversed is an uppercase character , Just use toLowerCase() Method to convert it to lowercase characters , Otherwise use toUpperCase() Method to convert it to uppercase characters .
(3) Define a StringBuilder object , call append() Method to add the traversal characters in turn , Last call StringBuider Object's toString() Method , And output the obtained results .

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());// Law 1 
			}
			if (ch[i] >= 'A' && ch[i] <= 'Z') {
    
				sb.append(str.toLowerCase().toCharArray()[i]);// Law two 
			}
		}
		System.out.print(sb.toString());
	}
}

原网站

版权声明
本文为[Xiaomurong]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221502448959.html