码迷,mamicode.com
首页 > 其他好文 > 详细

【程序49】

时间:2018-10-24 01:04:46      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:次数   else   str1   ++   bst   code   public   字符串   ||   

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

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】

标签:次数   else   str1   ++   bst   code   public   字符串   ||   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!