package homework;
import java.util.Scanner;
/*(1)從鍵盤(pán)循環(huán)錄入錄入一個(gè)字符串,輸入"end"表示結(jié)束
(2)將字符串中大寫(xiě)字母變成小寫(xiě)字母,小寫(xiě)字母變成大寫(xiě)字母,其它字符用"*"代替,并統(tǒng)計(jì)字母的個(gè)數(shù)
舉例:
鍵盤(pán)錄入:Hello12345World
輸出結(jié)果:hELLO*****wORLD
? 總共10個(gè)字母*/
public class Work3 {
public static void main(String[] args) {
String s = end();
System.out.println(s);
cast(s);
}
//利用stringbuffer的append和indexof功能,當(dāng)沒(méi)有索引的時(shí)候,indexof返回-1? 實(shí)現(xiàn)功能(1)
public static String end() {
StringBuffer str = new StringBuffer();
while (true) {
String a = new Scanner(System.in).next();
str.append(a);
if (str.indexOf("end") >= 0) {break;}
}
// System.out.println(str);
return str.toString();
}
//遍歷出字符串的每一個(gè)字符串,重新定義一個(gè)stringbuffer,每次都進(jìn)行處理
public static void cast(String s) {
StringBuffer str = new StringBuffer(s);
int num = 0;
for (int i =0;i<str.length();i++) {
if (str.charAt(i)>='a' && str.charAt(i)<='z') {
str = str.replace(i, i+1, str.substring(i, i+1).toUpperCase());
num++;
}
else if (str.charAt(i)>='A' && str.charAt(i)<='Z') {
str = str.replace(i, i+1, str.substring(i, i+1).toLowerCase());
num++;
}
else {
str = str.replace(i, i+1, "*");
}
}
/*{
if (str.charAt(i)>='a' && str.charAt(i)<='z') {
str.substring(i,1).toUpperCase();
num++;
}
else if (str.charAt(i)>='A' && str.charAt(i)<='Z') {
str.substring(i,1).toLowerCase();
num++;
}
else {
str.replace(i,1,"*");
}
}*/
System.out.println(str.toString());
System.out.println(num);
}
}