首页 > 其他 > 详细

【程序49】

时间:2018-10-24 00:10:16      阅读:118      评论:0      收藏:0      [点我收藏+]

题目:计算字符串中子串出现的次数

import java.util.*;

public class lianxi49 {

public static void main(String args[]){

    Scanner s = new Scanner(System.in);

    System.out.print("请输入字符串:");

    String str1 = s.nextLine();

    System.out.print("请输入子串:");

    String str2 = s.nextLine();

    int count=0; 

    if(str1.equals("")||str2.equals("")) 

    { 

       System.out.println("你没有输入字符串或子串,无法比较!"); 

       System.exit(0); 

    } 

    else 

    { 

        for(int i=0;i<=str1.length()-str2.length();i++) 

        { 

            if(str2.equals(str1.substring(i, str2.length()+i))) 

            //这种比法有问题,会把"aaa"看成有2个"aa"子串。 

            count++; 

        } 

    System.out.println("子串在字符串中出现: "+count+" 次"); 

    } 

}

}

```

【程序49】

原文:https://www.cnblogs.com/yuyu666/p/9840260.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!