任务描述
本关任务:接收给定的一个字符串,对该字符串做如下处理:
1.去除前后空格;
2.找出字符串中以字母 a 开头,以字母 z 结尾以及含有 li 的单词(如:alsliz);
3.输出字符串中共有多少个满足上述条件的单词;
4.将满足条件的单词分别转化为大写和小写输出。
编程要求
仔细阅读右侧编辑区内给出的代码框架及注释,按照提示编写程序代码。
测试说明
平台将使用测试集运行你编写的程序代码,若全部的运行结果正确,则通关。
可在右侧 “测试结果”区查看具体的测试集详情。
例:
测试输入:
asDliz,SDefs,ahdhjlid,sEfds
预期输出:
将符合条件的子字符串转化为小写:abcliz
将符合条件的子字符串转化为大写:ABCLIZ
将符合条件的子字符串转化为小写:alisdz
将符合条件的子字符串转化为大写:ALISDZ
字符串中共有符合条件的子字符串2个
测试输入:
asdlsd,sd,df,dfv,fAsw,ff
预期输出:
asdlsd,sd,df,dfv,fAsw,ff该字符串没有符合条件的子字符串
开始你的任务吧,祝你成
import java.util.Scanner;
public class StrTest {
public static void main(String[] args) {
// 请在Begin-End间编写代码
/********** Begin **********/
// 第一步:接收输入的字符串
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
// 第二步:对字符串做指定操作操作
str = str.trim();
String str1[] = str.split(","); //这部按照,来分出字符串数组
int count = 0;
for(int i = 0; i < str1.length; i++){
if(str1[i].startsWith("a")&&str1[i].endsWith("z")&&str1[i].contains("li")){
count++;
System.out.println("将符合条件的子字符串转化为小写:"+str1[i].toLowerCase());
System.out.println("将符合条件的子字符串转化为大写:"+str1[i].toUpperCase());
}
}
if(count==0){
System.out.print(str+"该字符串没有符合条件的子字符串");
}else{
System.out.println("字符串中共有符合条件的子字符串"+count+"个");
}
/********** End **********/
}
}
功!