• 第11次作业--字符串处理


    题目1编写一个应用程序,统计输入的一个字符串中相同字符的个数,并将统计结果输出。

    import java.util.Scanner;
    
    public class Text {
    
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner reader=new Scanner(System.in);
            String str=reader.nextLine();
            for(int i=0;i<str.length();i++){
                char c=str.charAt(i);//拆分
                String s=String.valueOf(c);//char转换成String
                if(str.indexOf(s)==i){//字符是否第一次出现
                    int num=0;
                    for(int j=0;j<str.length();j++){
                        if(str.regionMatches(j, s, 0, 1)){//查找字符个数
                            num++;
                        }
                    }
                    System.out.println(c+"出现了"+num);
                }
            }        
        }
    
    }

    运行界面

    题目2编写程序,输入一个字符串,判断该串中的字母能否组成一个回文串(回文串:一个字符串从前向后读取和从后向前读取都一样)。如:ab<c>c?ba

    import java.util.Scanner;
    public class Palindrome {
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner reader=new Scanner(System.in);
            String str=reader.nextLine();        
            StringBuffer str1=new StringBuffer(str);//将string转换成String'buffer
            str1.reverse();//逆置
            String str2=""+str1;
            if(str.equals(str2)){
                System.out.println("是回文");
            }else{
                System.out.println(str+"不是回文");
            }
        }
    
    }

    运行界面

  • 相关阅读:
    python selenium T5
    python selenium T4
    python selenium T3
    python selenium T2
    python selenium T1
    day1——变量,if语句 | day2——while循环
    Python Day48 mysql补充
    Python Day47索引
    Python Day46 MySQL数据备份
    Python Day45多表连接查询
  • 原文地址:https://www.cnblogs.com/mai98/p/11891837.html
Copyright © 2020-2023  润新知