package homework;
import java.util.Scanner;
/*(1)从键盘循环录入录入一个字符串,输入"end"表示结束
(2)将字符串中大写字母变成小写字母,小写字母变成大写字母,其它字符用"*"代替,并统计字母的个数
举例:
键盘录入:Hello12345World
输出结果:hELLO*****wORLD
总共10个字母*/
public class Work3 {
public static void main(String[] args) {
String s = end();
System.out.println(s);
cast(s);
}
//利用stringbuffer的append和indexof功能,当没有索引的时候,indexof返回-1 实现功能(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();
}
//遍历出字符串的每一个字符串,重新定义一个stringbuffer,每次都进行处理
public static void cast(String s) {
StringBuffer str = new StringBuffer(s);
int num = 0;
for (int i =0;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); } }